ArrayList in Java
- Unlike the standard array class in Java, the ArrayList is dynamic.
- It allows adding or removing the elements after it is created.
- Java ArrayList implements the List interface. That means you may use all operations that the list interface provides while ArrayList extends the AbstractList class.
- The index of ArrayList starts at 0.
- You may compare the ArrayList to the vector as well. The difference between the two is that the former is unsynchronized.
An example of creating an int type ArrayList:
ArrayList<Integer> arrlstInt = new ArrayList<>(); arrlstInt.add(1); arrlstInt.add(2);
I will explain more about ArrayList, first, have a look at a few examples of using this in Java programs.
An example of a string ArrayList
In this example, the ArrayList Java class is created with four elements.
First, arrlstStr object is declared as a String type by using the new keyword.
After that, the add method is used for creating the ArrayList items. Finally, a for loop is used for displaying the ArrayList elements:
Java code:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<String> arrlstStr = new ArrayList<String>(); //Declaring ArrayList arrlstStr.add("This"); arrlstStr.add("is"); arrlstStr.add("ArrayList"); arrlstStr.add("Tutorial!"); //Displaying array list for (int i=0;i<arrlstStr.size();i++){ System.out.println(arrlstStr.get(i)); } } }
Output:
is
ArrayList
Tutorial!
An example of integer type ArrayList
See this example code where a Java ArrayList is created with the integer type. The enhanced for loop is used for displaying the int ArrayList elements:
Java program:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<Integer> arrlstInt = new ArrayList<Integer>(); //Declaring ArrayList arrlstInt.add(1); arrlstInt.add(2); arrlstInt.add(3); arrlstInt.add(4); //Displaying array list for (int currArrLstElement : arrlstInt) { System.out.println("The ArrayList item: " + currArrLstElement); } } }
Output:
The ArrayList item: 2
The ArrayList item: 3
The ArrayList item: 4
A few main points about creating and accessing ArrayList Java class
The ArrayList can be created in a non-generic way (without specifying the type), for example:
So, the ArrayList class is followed by the name of your choice on the left side. On the right side, the new keyword is used which is followed by ArrayList class.
In that way, the ArrayList may contain different types of elements e.g. String, integers, etc.
Generic way by using date type as creating ArrayList:
Adding items in ArrayList
After declaring ArrayList, you may use the add method of ArrayList class to add the items. You may also specify the position where an item should be added (see details below). For example:
For adding an item in a specific position:
Accessing ArrayList elements
After declaring and adding elements in ArrayList, you may access the elements by using a simple or enhanced for-each loop. Both of these ways are used in above two examples.
You may also use the iterator interface for that purpose.
See the following example where an ArrayList object is created and five items are added by using the add method.
After that, the iterator interface is used with a while loop for displaying the items:
Java code with an iterator:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<Integer> arrlstInt = new ArrayList<Integer>(2); //Declaring ArrayList arrlstInt.add(5); arrlstInt.add(10); arrlstInt.add(20); arrlstInt.add(40); arrlstInt.add(80); //Displaying array list System.out.println("The ArrayList items by using iterator interface:"); Iterator arr_iterator=arrlstInt.iterator(); while(arr_iterator.hasNext()){ System.out.println(arr_iterator.next()); } } }
Output:
5
10
20
40
80
A few useful methods in the ArrayList class
The ArrayList class has a number of methods to work with objects. Following are a few with examples.
The add method
By using the add method of ArrayList, you may add items in a sequence or by specifying the position.
See this example where four items are added initially. After that, a for loop is used for displaying the items in ArrayList.
This is followed by adding two more items; one at 0 index and the other at 3 index position. The for each loop is used again for displaying the ArrayList:
Code with add method:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<Integer> arrlstInt = new ArrayList<Integer>(); //Declaring ArrayList arrlstInt.add(10); arrlstInt.add(20); arrlstInt.add(40); arrlstInt.add(50); //Displaying array list System.out.println("The ArrayList before adding items at specific positions: "); for (int currArrLstElement : arrlstInt) { System.out.println("The ArrayList item: " + currArrLstElement); } System.out.println(" "); //Adding items at specific position arrlstInt.add(0, 5); arrlstInt.add(3, 30); System.out.println("The ArrayList after adding items at specific positions: "); for (int currArrLstElement : arrlstInt) { System.out.println("The ArrayList item: " + currArrLstElement); } } }
Output:
You can see, item 5 is displayed at position 0 while item 30 is at index 3 after using the for-each loop.
A demo of using the size method
To know the size of the Array list, you may use the size method of ArrayList class.
To demonstrate this method, I have created a list of four elements. The System.out.println is used to display the ArrayList length by using the size method.
This is followed by using the for loop where the size method is used to specify the maximum number of times the loop should run. In each iteration, the ArrayList item is displayed:
The code for using size method:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<Integer> arrlstInt = new ArrayList<Integer>(); //Declaring ArrayList arrlstInt.add(100); arrlstInt.add(200); arrlstInt.add(300); arrlstInt.add(400); System.out.println("The size of ArrayList is: " + arrlstInt.size()); //Displaying array list System.out.println("The array elements are:"); for (int x=0;x < arrlstInt.size();x++){ System.out.println(arrlstInt.get(x)); } } }
Result:
The array elements are:
100
200
300
400
A demo of using the get method for accessing ArrayList items
You may use the get() method of ArrayList class to access a specific element in the ArrayList.
See this example where I created an ArrayList of strings with four elements.
After that, the System.out.println is used for displaying the item at index 2. See the program code and output below:
The code:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<String> arrlstStr = new ArrayList<String>(); //Declaring ArrayList arrlstStr.add("This"); arrlstStr.add("is"); arrlstStr.add("ArrayList"); arrlstStr.add("Tutorial!"); System.out.println("The item at position 2 = " + arrlstStr.get(2)); } }
Output:
A demo of the ‘contains’ method for searching array list
The contains method can be used for searching Array List.
It returns a Boolean value. if the specified element is found, it returns true, otherwise false.
As you run this program, it will ask you to enter a search term and press the return key. If that element is found, it will display: “The item is found in ArrayList!”. If the item is not found, the following message will display:
The item does not exist!
Code to search ArrayList:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<String> arrlstStr = new ArrayList<String>(); //Declaring ArrayList arrlstStr.add("The"); arrlstStr.add("ArrayList"); arrlstStr.add("are"); arrlstStr.add("Dynamic"); System.out.println("Enter an item to search: "); Scanner scanner = new Scanner(System.in); String searchterm = scanner.nextLine(); if (arrlstStr.contains(searchterm)){ System.out.println("The item is found in ArrayList!"); } else { System.out.println("The item does not exist!"); } } }
Sample Output:
Try different words including the item names that exist in the ArrayList in your IDE.
An example of using addAll method
The addAll method appends the items in the given ArrayList or other collection. By append, it means the items are added at last of specified ArrayList.
See an example of using the addAll method. First, an ArrayList is created with three items. Another ArrayList with 3 items is created. After that, the addAll method is used to append the items of the second list to the first one. The items of first ArrayList are displayed before and after using the addAll method to show the difference:
The code:
import java.util.*; public class java_array { public static void main(String []args) { ArrayList<Integer> arrlst1 = new ArrayList<Integer>(); //Declaring First ArrayList arrlst1.add(1); arrlst1.add(2); arrlst1.add(3); ArrayList<Integer> arrlst2 = new ArrayList<Integer>(); //Declaring second ArrayList arrlst2.add(10); arrlst2.add(20); arrlst2.add(30); //Displaying array list System.out.println("The ArrayList before addAll:"); System.out.println(" "); for (int currArrLstElement : arrlst1) { System.out.println("The ArrayList item: " + currArrLstElement); } //Using the addAll method arrlst1.addAll(arrlst2); //Displaying ArrayList after addAll method System.out.println(" "); System.out.println("The ArrayList after addAll:"); System.out.println(" "); for (int currArrLstElement : arrlst1) { System.out.println("The ArrayList item: " + currArrLstElement); } } }
Output:
Summarizing the ArrayList in Java
- The ArrayList implements the List interface and extends the AbstractList class.
- The ArrayList is unsynchronized.
- The index of ArrayList starts at 0.
- The ArrayList is dynamic. That means you may add or remove elements after it is created.
- The instance of ArrayList has the capacity that is the size of the array for storing items in the list. The capacity grows automatically as elements are added in the list.
- The ArrayList class has methods like add, size, addAll, removeAll, retainAll, get etc. to work with ArrayList elements.