Advertisement



< Prev
Next >



Java ArrayList Class





ArrayList is a collection class that implements List Interface. Just like a standard array, ArrayList is also used to store similar elements. So, what is the difference between a standard array and an ArrayList?

In the case of a standard array, we must declare its size before we use it and once its size is declared, it's fixed. While ArrayList is like a dynamic array i.e. we don't need to declare its size, it grows as we add elements to it and it shrinks as we remove elements from it, during the runtime of a program.









Constructors :


1) ArrayList()
This constructor creates an empty ArrayList of a particular object type.
Example -
ArrayList<Integer> arrL= new ArrayList<Integer>();

This constructor example creates an ArrayList to hold Integer objects.



2) ArrayList(Collection <? extends E>c)
This constructor creates an ArrayList initialized with the elements of Collection c. Collection c must hold similar type of objects as the type declared by the ArrayList.
ArrayList<Integer> arrL1 = new ArrayList<Integer>():
ArrayList<Integer> arrL2 = new ArrayList<Integer>(arrL1);

This constructor example creates an ArrayList arrL2 initialized with the elements of an ArrayList arrL1. Both ArrayList hold Integer objects.




Some important methods of ArrayList class


Methods Description
boolean add(E e) This method adds the specified element e to the end of an ArrayList.
add(int index, E element This method adds the element at the specified index in an ArrayList.
int size() This method returns the total number of elements in an ArrayList.
void clear() This method removes all of the elements from the ArrayList
E get(int index) This method returns the element at the specified index in an ArrayList.
contains(Object o) This method checks if ArrayList contains a specific Object.
E remove(int index) This method removes an element at a specified index in an ArrayList.
boolean remove(Object o) This method removes the first occurrence of an Object from the ArrayList, if it is present.
Object[] toArray() This method returns an Object array containing all the elements of an ArrayList.
Iterator iterator() This method returns an Iterator to iterate over elements of an ArrayList.
ListIterator listIterator() This method returns a ListIterator to iterate over elements of an ArrayList.



Advertisement




Creating a ArrayList and displaying its features.


In this program we have created an ArrayList to hold <String> objects and we will add/remove some elements from this list.

//Java - Example of ArrayList
 
 
import java.util.*;

class ArrayList1
{
public static void main(String... ar)
{
ArrayList<String> arr= new ArrayList<String>();
System.out.println("Initial size of ArrayList = "+ arr.size());

arr.add("C");
arr.add("A");
arr.add("4");
arr.add("F");
arr.add("x");
arr.add("1");

System.out.println("ArrayList after adding objects = " + arr);
System.out.println("Size of ArrayList = "+ arr.size());

arr.remove(4);	//Removing an element at the 4th index

System.out.println("ArrayList afer removing element at 4th index = "+ arr);

arr.remove("1"); //Removing an element "1"

System.out.println("ArrayList afer removing element 1 = "+ arr);
System.out.println("Size of  ArrayList = "+ arr.size());
}

}


Output is - :


Initial size of ArrayList = 0
ArrayList after adding objects = [C, A, 4, F, x, 1]
Size of ArrayList = 6
ArrayList afer removing element at 4th index = [C, A, 4, F, 1]
ArrayList afer removing element 1 = [C, A, 4, F]
Size of new ArrayList = 4


Program Analysis







Creating a new ArrayList from an existing ArrayList


In this program we will create an ArrayList that holds <Integer> objects. We will create and initialize a new ArrayList by using the contents of an existing ArrayList.

//Java - Example of ArrayList
  
  
import java.util.*;

class ArrayList1
{
public static void main(String... ar)
{
ArrayList<Integer> array1= new ArrayList<Integer>();

array1.add(4);
array1.add(1);
array1.add(5);
array1.add(2);
array1.add(3);


System.out.println("ArrayList after adding objects = " + array1);
System.out.println("Size of ArrayList = "+ array1.size());

System.out.println("Creating a new ArrayList created from an existing one");

ArrayList<Integer> array2= new ArrayList<Integer>(array1);
System.out.println("Contents of new ArrayList : " + array2);
System.out.println("Size of new ArrayList = "+ array2.size());
}

}


Output is - :


ArrayList after adding objects = [4, 1, 5, 2, 3]
Size of ArrayList = 5
Creating a new ArrayList created from an existing ArrayList
Contents of new ArrayList : [4, 1, 5, 2, 3]
Size of new ArrayList = 5

By using ArrayList(array1), a new ArrayList(array2) is created, this will initialize new ArrayList(array2) with the elements of existing ArrayList(array1).




Converting an ArrayList to an Object array by using toArray() method.


In the next example, we are going to create an Object array from an existing ArrayList by using toArray() method of ArrayList class.

//Java - Example of ArrayList


import java.util.*;

class ArrayList1
{
public static void main(String... ar)
{
ArrayList<Integer> array1= new ArrayList<Integer>();

array1.add(4);
array1.add(1);
array1.add(5);
array1.add(2);
array1.add(3);


System.out.println("ArrayList after adding objects = " + array1);
System.out.println("Size of ArrayList = "+ array1.size());

//Converting an ArrayList to an Object array.
Object[] a = array1.toArray();

System.out.print("Contents of an Object Array created from ArrayList - ");

//Iterating over the elements of Object array
for(Object element : a)
System.out.print(element + " " );

}
}



Output is - :


ArrayList after adding objects = [4, 1, 5, 2, 3]
Size of ArrayList = 5
Contents of an Object Array after created from ArrayList - 4 1 5 2 3

After creating an Object array from an existing ArrayList by using toArray() method of ArrayList class, we have displayed the elements in this Object array by using a for-each loop.



Please share this article -





< Prev
Next >
< Swing Applet
LinkedList Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement