Collection Framework - ArrayList class
Arrays are of fixed length. Once created they cannot grow or shrink.
Collection framework provides ArrayList that supports dynamic arrays that can change in size as needed.
Array Lists are created with an initial size of 10. When the size increases, a new array list is created and all the objects are copied to that, and reference is set to that arraylist.
Similarly, when the objects are removed, size can be reduced.
ArrayList comes with 3 constructors :
ArrayList() - creates an arraylist with initialcapacity of 10.
ArrayList(Collection<? extends E> coll) - creates an arraylist from any collection derived class like linkedlist, treeset, vector, etc.
ArrayList(int initialCapacity) - creates an arraylist with the specified initial capacity.
An example for all the above constructor types can be found here
Also starting with Java 5, Collection framework supports generics so the above example also shows that implementation as well.
Collection framework provides ArrayList that supports dynamic arrays that can change in size as needed.
Array Lists are created with an initial size of 10. When the size increases, a new array list is created and all the objects are copied to that, and reference is set to that arraylist.
Similarly, when the objects are removed, size can be reduced.
ArrayList comes with 3 constructors :
ArrayList() - creates an arraylist with initialcapacity of 10.
ArrayList(Collection<? extends E> coll) - creates an arraylist from any collection derived class like linkedlist, treeset, vector, etc.
ArrayList(int initialCapacity) - creates an arraylist with the specified initial capacity.
An example for all the above constructor types can be found here
Also starting with Java 5, Collection framework supports generics so the above example also shows that implementation as well.
Comments
Post a Comment