Java Tutorial-Java ArrayList

Java ArrayList


In Java, ArrayList is a resizable array, which can be found in java.util package.

The difference between built-in array and ArrayList in Java, is that the size of array cannot be modified (if you want to add or remove elements  to/from array,  you have to create a new one).  The elements can be added and removed from an ArrayList whenever needed. The syntax is also slightly different:


Syntax

Create an ArrayList object called colors that will store strings:

import java.util.ArrayList; // import the ArrayList class
ArrayList<String> colors = new ArrayList<String>(); //Create an ArrayList object

Add Items to ArrayList

The ArrayList class contains many useful methods. Lets add the elements to the ArrayList with the add() method:


Example

import java.util.ArrayList; // import the ArrayList class
 
public class ArrayListExample {
       public static void main(String args[]){
              ArrayList<String> colors = new ArrayList<String>(); //Create an ArrayList object
              colors.add("Blue");
              colors.add("Red");
              colors.add("Green");
              System.out.println(colors);
       }
}

Output

[Blue, Red, Green]

Access an Item

To access an element in the ArrayList, use the get() method and refer to the index number.


Example


colors.get(1);


Note: The array index starts from 0. [0] is the first element, [1] is the second element, etc



Change an Item

To modify an element in the ArrayList, use the set() method and refer to the index number.


Example

colors.set(1, "Pink");

Remove an Item

To delete an element in the ArrayList, use the remove() method and refer to the  index number


Example

colors.remove(2);

To remove all the elements in the ArrayList, use the clear() method.


Example

colors.clear();

ArrayList size

To find how many elements in the ArrayList, use the size() method.


Example

colors.size();

Loop through an ArrayList

We can loop through the elements of an ArrayList with a for loop, use the size() method to specify how many times the loop should run.


Example

import java.util.ArrayList; // import the ArrayList class
 
public class ArrayListExample {
       public static void main(String args[]){
              ArrayList<String> colors = new ArrayList<String>(); //Create an ArrayList object
              colors.add("Blue");
              colors.add("Red");
              colors.add("Green");
              colors.add("Black");
              for (int i = 0; i < colors.size(); i++){
                     System.out.println(colors.get(i));
              }
       }
}

Output

Blue
Red
Green
Black

We can also loop through element in an ArrayList using for-each loop.


Example

import java.util.ArrayList; // import the ArrayList class
 
public class ArrayListExample {
       public static void main(String args[]){
              ArrayList<String> colors = new ArrayList<String>(); //Create an ArrayList object
              colors.add("Blue");
              colors.add("Red");
              colors.add("Green");
              colors.add("Black");
              for (String i : colors){
                     System.out.println(i);
              }
       }
}

Output

Blue
Red
Green
Black

Other Types of ArrayList


Elements in an ArrayList are actually objects. In the above example, we created elements (objects) of the type “String”. Remember that a String in Java is an object (not a primitive type). To use other types, we must specify and equivalent wrapper class. For primitive type, use Integer for int, Boolean for Boolean, Double for double, etc.


Example 

import java.util.ArrayList;
 
public class IntArrayExample {
       public static void main(String args[]){
              ArrayList<Integer> numbers = new ArrayList<Integer>();
              numbers.add(10);
              numbers.add(15);
              numbers.add(20);
              numbers.add(30);
             
              for(int i:numbers){
                     System.out.println(i);
              }
       }
}

Output

10
15
20
30

Sort an ArrayList


Another useful class in the java.util package is the Collection class, which include sort() method for sorting ArrayList alphabetically or numerically.


Example

In the below example, we sort the colors (Strings):

import java.util.ArrayList;
import java.util.Collections; //import the Collection class
 
public class ArrayListExample {
       public static void main(String args[]){
              ArrayList<String> colors = new ArrayList<String>(); //Create an ArrayList object
              colors.add("Blue");
              colors.add("Red");
              colors.add("Green");
              colors.add("Black");
             
              Collections.sort(colors);
             
              for (String i : colors){
                     System.out.println(i);
              }
       }
}

Output

Black
Blue
Green
Red

Example


In the below example, we sort the numbers (Integers):

import java.util.ArrayList;
import java.util.Collections; //Import the Collections class
 
public class IntArrayExample {
       public static void main(String args[]){
              ArrayList<Integer> numbers = new ArrayList<Integer>();
              numbers.add(40);
              numbers.add(15);
              numbers.add(90);
              numbers.add(30);
             
              Collections.sort(numbers);
             
              for(int i:numbers){
                     System.out.println(i);
              }
       }
}

Output

15
30
40
90