In this example we are going to show the use of java.util.ArrayList. We will be creatiing an object of ArrayList class and performs various operations like adding removing the objects.
Arraylist provides methods to manipulate the size of the array that is used internally to store the list. ArrayList extends AbstractList and implements List, Cloneable, Serializable. ArrayList capacity .grows automatically. The ArrayList is not synchronized. It permits all elements including null.
In this program we are inserting a value. We are using three methods of ArrayList class.
add(Object o): Appends the specified element to the end of this list. It returns a boolean value.
size(): Returns the number of elements in this list.
remove(int index): Removes the element at the specified position in this list. It returns the element that was removed from the list. It throws IndexOutOfBoundsException : if index is out of range.
Code of a program is given below:
The output of program will be like this:
|C:\Java Tutorial>javac ArrayListDemo.java
C:\Java Tutorial>java ArrayListDemo
The content of arraylist is: 
The size of an arraylist is: 0
The content of arraylist is: [10, 20, tapan]
The size of an arraylist is: 3
The content of arraylist is: [10, 20, tapan, 10, 20, 30, 40, 50]
The size of an arraylist is: 8
The clone is: [10, 20, tapan, 20, 30, 40, 50]
The content of arraylist is: [10, 20, tapan, 20, 30, 40, 50]
The size of an arraylist is: 7
We have 1000s of tutorials on our website. Search Tutorials tutorials on our website.
Posted on: June 7, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles