ArrayList in Java


Standard java arrays size is fixed. Once the arrays are created, they can not grow or shrink which means you can not change the size of the array. You must now the requirement of your program to initialize the array of required size.
To overcome such limitations, java provides the ArrayList class that can store unlimited number of objects. The ArrayList class extends AbstractList and implements the List interface.

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)

Defines an an empty list with the specified initial capacity.

ArrayList supports large number of methods, Few of them are specified in table below.

void add(int index, Object element)

Inserts the specified element at given position in this list

boolean add(Object o)

Appends the specified element to the end of this list

boolean addAll(Collection c)

Appends all of the elements in the specified collection to the end of this list

boolean addAll(int index, Collection c)

Inserts all of the elements in the specified collection into this list, starting from given position

void clear()

Removes all of the elements from this list

Object clone()

Returns a shallow copy of this ArrayList

boolean contains(Object o)

Returns true if this list contains the specified element

Object get(int index)

Returns the element at the specified position in this list

int indexOf(Object o)

Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element

Object remove(int index)

Removes the element at the specified position in this list

int lastIndexOf(Object o)

Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element

Object set(int index, Object element)

Replaces the element at the specified position in this list with the specified element

int size()

Returns the number of elements in this list

Object[] toArray()

Returns an array containing all of the elements in this list in the correct order

Object[] toArray(Object[] a)

Returns an array containing all of the elements in this list

Example

import java.util.ArrayList;

public class ArrayListDemo {
	public static void main(String[] args) {
		// List of programming languages
		ArrayList list = new ArrayList();

		System.out.println("(1). Size of the list :" + list.size());
		list.add("Java");
		list.add("C++");
		list.add("C#");
		list.add("C");
		list.add("PHP");
		list.add("ASP");
		System.out.println("(2). Size of the list :" + list.size());
		System.out.println("(3). list contains Java :" + list.contains("Java"));
		System.out.println("(4). List toString :" + list.toString());

		// Remove element from index 1
		list.remove(1);

		list.remove("ASP");

		System.out.println("(5). Size of the list :" + list.size());
		System.out.println("(6). list contains ASP :" + list.contains("ASP"));
		System.out.println("(7). List toString :" + list.toString());

	}
}

Output

(1). Size of the list :0
(2). Size of the list :6
(3). list contains Java :true
(4). List toString :[Java, C++, C#, C, PHP, ASP]
(5). Size of the list :4
(6). list contains ASP :false
(7). List toString :[Java, C#, C, PHP]

No Responses