In this Java Tutorial, you will learn how to print all the elements of an ArrayList using For loop, or ArrayList.forEach() method, with examples.

Print Elements of ArrayList

There are many ways to print elements of an ArrayList. In this tutorial, we will go through the following processes.

Process 1: Java For Loop can be used to iterate through all the elements of an ArrayList.

Process 2: Java provides forEach(); method for ArrayList. Each element can be accessed using the parameter provided inside the forEach() function.

Examples

1. Print All Elements of ArrayList – For Loop

In the following example, we will initialize an ArrayList with some elements and print them using for loop.

PrintElements.java

</>
Copy
import java.util.ArrayList;

public class PrintElements {

	public static void main(String[] args) {
		ArrayList<String> names = new ArrayList<String>();
		names.add("Java");
		names.add("Kotlin");
		names.add("Android");
		
		for(String name:names) {
			System.out.println(name);
		}
	}
}

Output

Java
Kotlin
Android

2. Print All Elements of ArrayList – ArrayList.forEach()

In the following example, we will initialize an ArrayList with some elements and print them using ArrayList.forEach() method.

PrintElements.java

</>
Copy
import java.util.ArrayList;

public class PrintElements {

	public static void main(String[] args) {
		ArrayList<String> names = new ArrayList<String>();
		
		names.add("Java");
		names.add("Kotlin");
		names.add("Android");
		
		names.forEach(name ->{
			System.out.println(name);
		});
	}
}

Output

Java
Kotlin
Android

Conclusion

In this Java Tutorial, we learned how to print elements of an ArrayList using for loop and ArrayList.forEach() method.