ArrayList

< Java Programming

Navigate Aggregate 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.

Initializing

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(Collection c)

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(int capacity)

Methods

ArrayList defines following methods:

Adding Element in ArrayList

void add(int index, Object element)
boolean add(Object o)
boolean addAll(Collection c)
boolean addAll(int index, Collection c)

Size of ArrayList

int size()

Adding Element and Size of ArrayList

 
import java.util.*;

public class ArrayListDemo{
	public static void main(String[] args) {
		// create an array list
		ArrayList al= new ArrayList();
		System.out.println("Initial ArrayList : "+al);
		
		 // add elements to the array list
		al.add("A");
		al.add("B");
		
		//find size of ArrayList
		System.out.println("Size of al :"+al.size());
		// display the array list
		System.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 :2
Contents of al :[A, B]
Contents of al :[A, C, B]
Size of al :3


Get and Set ArrayList Element

Object get(int index)
Object set(int index, Object element)

Find Index of ArrayList Element

int indexOf(Object o)
int lastIndexOf(Object o)

Find Element Contain in ArrayList

boolean contains(Object o)

Different Method in ArrayList

 
public class ArrayListDemo {
	public static void main(String[] args) {
		// create an array list
		ArrayList al = new ArrayList();

		// add elements to the array list
		al.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 ArrayList
		System.out.println("Index of D : " + al.indexOf("D"));
		System.out.println("Index of A : " + al.indexOf("A"));

		// find index of element in ArrayList
		System.out.println("Index of A : " + al.lastIndexOf("A"));

		// get element at given Index
		System.out.println("Element at Second Index : " + al.get(2));
		System.out.println("Element at Sixth Index : " + al.get(6));
		
		//set element at given Index
		al.set(3,"B"); // replacing third index element by "B"
		System.out.println("Contents of al : " + al);
		
		//check ArrayList contains given element
		System.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 : 4
Index of A : 0
Index of A : 5
Element at Second Index : C
Element at Sixth Index : E
Contents of al : [A, B, C, B, D, A, E]
ArrayList contain D : true
ArrayList contain F : false
Test your knowledge

Question: Consider the following code:

public class ArrayListDemo {
		public static void main(String[] args) {
	
		ArrayList al = new ArrayList();

		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?

Answer
Size of al : 4
Contents of al : [A, G, E, I]

Some more ArrayList methods:

MethodDescription
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. Throws NullPointerException 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.
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.