An ArrayList Class is a resizable array that is present in java.util package. An ArrayList can be printed in the following ways.

Printing ArrayList using a print statement

The ArrayList can be printed by using the Java print statement.

The example below illustrates how to print the elements of ArrayList using a print statement.

import java.util.ArrayList;
public class Example {
  public static void main(String[] args) {
    ArrayList < Integer > al = new ArrayList < Integer > ();
    al.add(10);
    al.add(20);
    al.add(30);
    al.add(40);
    // printing ArrayList elements using print statement
    System.out.print(al);
  }
}

The output is

[10, 20, 30, 40]

Printing ArrayList using Arrays.toString

The ArrayList can be printed using the Arrays.toString() method. The ArrayList is converted to an array using the toArray() method and then printed using the Arrays.toString() method.

The example program illustrates how to print elements of ArrayList using the Arrays.toString() method.

import java.util.ArrayList;
import java.util.Arrays;
public class Example {
  public static void main(String[] args) {
    ArrayList < String > ar = new ArrayList < String > ();
    ar.add("Audi");
    ar.add("BMW");
    ar.add("Volvo");
    ar.add("Tesla");
    // printing ArrayList elements using Arrays.toString 
    System.out.print(Arrays.toString(ar.toArray()));
  }
}

The output is

[Audi, BMW, Volvo, Tesla]

Printing ArrayList using for loop

An ArrayList can be printed using for loop. The for loop iterates through each element from starting index to the last index.

The example below illustrates how to print ArrayList using for loop.

import java.util.ArrayList;
public class Example {
  public static void main(String[] args) {
    ArrayList < Integer > arr = new ArrayList < Integer > ();
    arr.add(4);
    arr.add(9);
    arr.add(16);
    arr.add(25);
    // printing ArrayList using for loop
    for (int i = 0; i < arr.size(); i++) {
      System.out.println(arr.get(i));
    }
  }
}

The output is

4
9
16
25

Printing ArrayList using for each loop

An ArrayList can be printed using for each loop. The get() method is not required to print ArrayList when we use for each loop.

The example below illustrates how to print elements of ArrayList using for each loop.

import java.util.ArrayList;
public class Example {
  public static void main(String[] args) {
    ArrayList < String > str = new ArrayList < String > ();
    str.add("Alice");
    str.add("Bob");
    str.add("Joe");
    str.add("Roy");
    // printing ArrayList using for each loop
    for (String x: str) {
      System.out.println(x);
    }
  }
}

The output is

Alice
Bob
Joe
Roy

Printing ArrayList using Iterator/ListIterator

An ArrayList can be printed using an Iterator or a ListIterator. Both Iterator and ListIterator can be printed using the hasNext() and next() methods.

The example below illustrates how to print ArrayList using an Iterator.

import java.util.*;
public class Example {
  public static void main(String[] args) {
    ArrayList < String > arry = new ArrayList < String > ();
    arry.add("Aerospace");
    arry.add("Computer Science");
    arry.add("Information Science");
    arry.add("Mechanical");
    // to print using ListIterator follow the declaration
    // ListIterator itr=arry.listIterator();
    Iterator itr = arry.iterator();
    while (itr.hasNext()) {
      System.out.println(itr.next());
    }
  }
}

The output is

Aerospace
Computer Science
Information Science
Mechanical
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions