![]() | Java Programming ArrayList | Map![]() |
NavigateAggregate topic:() |
The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you 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.
The ArrayList class supports three constructors. The first constructor builds an empty array list.:
ArrayList()
The following constructor builds an array list that is initialized with the elements of the collection c.
ArrayList(Collectionc)
The following constructor builds an array list that has the specified initial capacity. The capacity is the size of the underlying array that is used to store the elements.
The capacity grows automatically as elements are added to an array list.
ArrayList(intcapacity)
ArrayList defines following methods:
set
method with the same parameters). ThrowsIndexOutOfBoundsException
if the specified index is out of range (index < 0 || index >= size()).voidadd(intindex,Objectelement)
booleanadd(Objecto)
NullPointerException
if the specified collection is null.booleanaddAll(Collectionc)
NullPointerException
if the specified collection is null.booleanaddAll(intindex,Collectionc)
intsize()
Adding Element and Size of ArrayList
importjava.util.*;publicclassArrayListDemo{publicstaticvoidmain(String[]args){// create an array listArrayList<String>al=newArrayList<String>();System.out.println("Initial ArrayList : "+al);// add elements to the array listal.add("A");al.add("B");//find size of ArrayListSystem.out.println("Size of al :"+al.size());// display the array listSystem.out.println("Contents of al :"+al);al.add(1,"C");System.out.println("Contents of al :"+al);System.out.println("Size of al :"+al.size());}}
Output for Adding Element and Size of ArrayList
![]() | Initial ArrayList : []Size of al :2Contents of al :[A, B]Contents of al :[A, C, B]Size of al :3 |
IndexOutOfBoundsException
if the specified index is out of range (index < 0 or index >= size()).Objectget(intindex)
IndexOutOfBoundsException
if the specified index is is out of range (index < 0 or index >= size()).Objectset(intindex,Objectelement)
intindexOf(Objecto)
intlastIndexOf(Objecto)
true
if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).booleancontains(Objecto)
Different Method in ArrayList
publicclassArrayListDemo{publicstaticvoidmain(String[]args){// create an array listArrayListal=newArrayList();// add elements to the array listal.add("A");al.add("B");al.add("C");al.add("A");al.add("D");al.add("A");al.add("E");System.out.println("Contents of al : "+al);// find index of element in ArrayListSystem.out.println("Index of D : "+al.indexOf("D"));System.out.println("Index of A : "+al.indexOf("A"));// find index of element in ArrayListSystem.out.println("Index of A : "+al.lastIndexOf("A"));// get element at given IndexSystem.out.println("Element at Second Index : "+al.get(2));System.out.println("Element at Sixth Index : "+al.get(6));//set element at given Indexal.set(3,"B");// replacing third index element by "B"System.out.println("Contents of al : "+al);//check ArrayList contains given elementSystem.out.println("ArrayList contain D : "+al.contains("D"));System.out.println("ArrayList contain F : "+al.contains("F"));}}
Output for Different Method in ArrayList
![]() | Contents of al : [A, B, C, A, D, A, E]Index of D : 4Index of A : 0Index of A : 5Element at Second Index : CElement at Sixth Index : EContents of al : [A, B, C, B, D, A, E]ArrayList contain D : trueArrayList contain F : false |
Question: Consider the following code:
![]() | publicclassArrayListDemo{publicstaticvoidmain(String[]args){ArrayListal=newArrayList();al.add("A");al.add("B");al.add("C");al.add("E");al.add("F");al.remove(2);al.remove("F");al.set(1,"G");al.add("H");al.set(3,"I");System.out.println("Size of al : "+al.size());System.out.println("Contents of al : "+al);}} |
In the example above, what is output?
![]() | Size of al : 4Contents of al : [A, G, E, I] |
Some more ArrayList methods:
Method | Description |
---|---|
Object clone() | Returns a shallow copy of this ArrayList. |
Object[] toArray() | Returns an array containing all of the elements in this list in the correct order. ThrowsNullPointerException if the specified array is null. |
void trimToSize() | Trims the capacity of this ArrayList instance to be the list's current size. |
void ensureCapacity(int minCapacity) | Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument. |
protected void removeRange(int fromIndex, int toIndex) | Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. |
![]() | Java Programming ArrayList | Map![]() |