Java ArrayList: explained with 8 examples
The ArrayList in Java
Unlike the standard array class in Java, the ArrayList is dynamic that allows adding or removing the elements after it is created. The Java ArrayList implements the List interface. That means you may use all operation that list interface provides while ArrayList extends the AbstractList class.
You may compare the ArrayList to the vector as well. The difference between the two is that the former is unsynchronized.
I will explain further about ArrayList, first, have a look at a few examples of using this in Java programs.
An example of string ArrayList
In this example, ArrayList Java class is created with four elements. First, arrlstStr object is declared as 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:
The ArrayList program code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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:
Note: You have to import the java.util.ArrayList or java.util.* in order to work with ArrayList.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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:
A few main points about creating and accessing ArrayList Java class
The ArrayList can be created in non-generic way (without specifying the type), for example:
ArrayList arrlstGen = new ArrayList ();
So, 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.
You may also use generic way by using date type as creating Java ArrayList:
ArrayList<Integer> arrlstInt = new ArrayList<Integer>();
This method is used in both of the above examples. If you try any other type for element value then an error will be generated.
Adding items in ArrayList
After declaring ArrayList, you may use the add method of ArrayList class for adding the items. You may also specify the position where an item should be added (see details below). For example:
arrlstInt.add(3);
For adding an item in specific position:
arrlstInt.add(3, 25);
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 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 while loop for displaying the items:
Java code with iterator:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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:
A few useful methods in 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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 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 a 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 size method is used to specify the maximum number of times loop should run. In each iteration, the ArrayList item is displayed:
The code of using size method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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:
A demo of using get method for accessing ArrayList items
You may use the get() method of ArrayList class for accessing 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 by clicking the link or image below:
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
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 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 to enter a search term and press return key. If that element is found, it will display: “The item is found in ArrayList!”. If item is not found, the following message will display:
The item does not exist!
Code to search ArrayList:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
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!"); } } } |
Output:
Try different words including the item names 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
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.