The ArrayList class extends AbstractList and implements the List interface.
ArrayList supports both Iterator and ListIterator for iteration but it’s recommended to use ListIterator as it allows the programmer to traverse the list in either direction, modify the list during iteration, and obtain the Iterator's current position in the list. But while using ListIterator you need to be little careful because ListIterator has no current element; its cursor position always lies between the element that would be returned by a call to previous () and the element that would be returned by a call to next ().
Features of ArrayList:
1. Uses the dynamic array for storing elements.
2. It may contain duplicate elements.
3. Maintains the order of insertion.
4. It not synchronized class.
5. It supports the random access because we can acceess the elements on index basis.
6. It is slow because shifting needs to be oocured.
Following are some differences between Java Array and Arraylist:
ArrayList supports dynamic arrays that can grow as needed.
Where as Java arrays are of a fixed length that means once we created the arrays, they cannot grow or shrink. So we must know in advance how many elements an array will hold.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. This is not possible in java arrays.
The ArrayList class supports following three constructors.
ArrayList( ) -- Creates an empty array list.
ArrayList(Collection c) -- Creates an array list that is initialized with the elements of the collection c.
ArrayList(int capacity) -- Creates an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.
Example:
/**
* List allows Duplicate values
*/
List list = new ArrayList();
list.add("krishna");
list.add("krishna1");
list.add("krishna1");
list.add("krishna");
list.add(new String("anusha"));
list.add(new String("anusha"));
list.add(new Integer(8));
System.out.println(list.toArray().length);
System.out.println(list);
ArrayList uses a dynamic array for storing the elements. ArrayList represents an automatic re-sizable array. Since we can not modify size of an array after creating it, we prefer to use ArrayList in Java which re-size itself automatically once it gets full. ArrayList in Java extends AbstractList and implements List interface and allows null values. Java ArrayList also maintains insertion order of elements and allows duplicates. We have random access of ArrayList because array works at the index basis.
Hierarchy of ArrayList Class:
ArrayList has been modified in Java5 to support Generics which makes Java ArrayList even more powerful because of enhanced type-safety. Before Java5 since there was no generics no type checking at compile time which means there is chance of storing different type of element in an ArrayList which is meant for something and ultimately results in ClassCastException during runtime. with generics you can create Java ArrayList which accepts only type of object specified during creation time and results in compilation error if someone tries to insert any other object into ArrayList in Java; for example if you create an ArrayList of String object you can not store Integer on it because add() method of ArrayList will check Type before adding object into ArrayList in Java5.
Features of ArrayList:
1. Uses the dynamic array for storing elements.
2. It may contain duplicate elements.
3. Maintains the order of insertion.
4. It not synchronized class.
5. It supports the random access because we can acceess the elements on index basis.
6. It is slow because shifting needs to be oocured.
Following are some differences between Java Array and Arraylist:
ArrayList supports dynamic arrays that can grow as needed.
Where as Java arrays are of a fixed length that means once we created the arrays, they cannot grow or shrink. So we must know in advance how many elements an array will hold.
Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. This is not possible in java arrays.
The ArrayList class supports following three constructors.
ArrayList( ) -- Creates an empty array list.
ArrayList(Collection c) -- Creates an array list that is initialized with the elements of the collection c.
ArrayList(int capacity) -- Creates an array list that has the specified initial capacity. The capacity grows automatically as elements are added to an array list.
Example:
/**
* List allows Duplicate values
*/
List list = new ArrayList();
list.add("krishna");
list.add("krishna1");
list.add("krishna1");
list.add("krishna");
list.add(new String("anusha"));
list.add(new String("anusha"));
list.add(new Integer(8));
System.out.println(list.toArray().length);
System.out.println(list);
No comments:
Post a Comment