Hit enter after type your search item

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:


Output:

arraylist

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:


Output:

arraylist integer

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:


Output:

arraylist iterator

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:


Output:

arraylist add

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:


Result:

arraylist size

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:


Output:

arraylist get

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:


Output:

arraylist contains

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:


Output:

arraylist addall

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.
This div height required for enabling the sticky sidebar