How to Print all Elements of an Arraylist in Java

Overview

In this tutorial, we show you how to print all elements of an ArrayList in Java. In this example below, we will display all the elements of a ArrayList using Loop.


How to Print all Elements of an Arraylist in Java

Print All Elements of an ArrayList using for (Object object : list)

package com.jackrutorial;

import java.util.ArrayList;
import java.util.List;

public class PrintAllElementsOfAnArraylistExample1 {

 public static void main(String[] args) {
  List<String> pgLangs = new ArrayList<String>();
  pgLangs.add("Java");
  pgLangs.add("AngularJS");
  pgLangs.add("ReactJS");
  pgLangs.add("Spring MVC");
  
  for (String lang : pgLangs) {
   System.out.println(lang);
  }
 }
}

Output

Java
AngularJS
ReactJS
Spring MVC

Print All Elements of an ArrayList using Loop for iterating ArrayList

package com.jackrutorial;

import java.util.ArrayList;
import java.util.List;

public class PrintAllElementsOfAnArraylistExample2 {
 
 public static void main(String[] args) {
  List pgLangs = new ArrayList();
  pgLangs.add("Java");
  pgLangs.add("AngularJS");
  pgLangs.add("ReactJS");
  pgLangs.add("Spring MVC");
  
  for (int i = 0; i < pgLangs.size(); i++) {
   System.out.println(pgLangs.get(i));
  }
 }
}

Output

Java
AngularJS
ReactJS
Spring MVC

Print All Elements of an ArrayList using ArrayList.iterator()

package com.jackrutorial;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class PrintAllElementsOfAnArraylistExample3 {

 public static void main(String[] args) {
  List<String> pgLangs = new ArrayList<String>();
  pgLangs.add("Java");
  pgLangs.add("AngularJS");
  pgLangs.add("ReactJS");
  pgLangs.add("Spring MVC");
  
  Iterator<String> iterator = pgLangs.iterator();
  while (iterator.hasNext()) {
   System.out.println(iterator.next());
  }
 }
}

Output

Java
AngularJS
ReactJS
Spring MVC

Print All Elements of an ArrayList using java.util.ArrayList.forEach() in JDK 8

package com.jackrutorial;

import java.util.ArrayList;
import java.util.List;

public class PrintAllElementsOfAnArraylistExample4 {

 public static void main(String[] args) {
  List pgLangs = new ArrayList();
  pgLangs.add("Java");
  pgLangs.add("AngularJS");
  pgLangs.add("ReactJS");
  pgLangs.add("Spring MVC");
  
  //lambda expression
  pgLangs.forEach(s -> System.out.println(s));
  //method reference
  pgLangs.forEach(System.out::println);
 }
}

Output

Java
AngularJS
ReactJS
Spring MVC
Java
AngularJS
ReactJS
Spring MVC

Print All Elements of an ArrayList using while() loop

package com.jackrutorial;

import java.util.ArrayList;
import java.util.List;

public class PrintAllElementsOfAnArraylistExample5 {

 public static void main(String[] args) {
  List pgLangs = new ArrayList();
  pgLangs.add("Java");
  pgLangs.add("AngularJS");
  pgLangs.add("ReactJS");
  pgLangs.add("Spring MVC");
  
  int i = 0;
  while (pgLangs.size() > i) {
   System.out.println(pgLangs.get(i));
   i++;
  }
 }
}

Output

Java
AngularJS
ReactJS
Spring MVC
Previous Post
Next Post

post written by: