Mejor respuesta
 import java.util.ArrayList; 
 import org.testng.annotations.Test; 
 public class arrayList { 
  @Test 
  public void arrayListTest() { 
 // Define array list which can hold array list objects  
 // of type any (String/Integer/Boolean) 
 ArrayList 
   
  // Define array list 1 and add data to it 
  ArrayList 
  innerInt1.add(100);  
  innerInt1.add(300); 
  innerInt1.add(500); 
  // Define array list 2 and add data to it 
  ArrayList 
  innerInt2.add(200);  
  innerInt2.add(400); 
  innerInt2.add(600); 
 // Add inner array list objects created to outer array list  
 // Outer array declaration says it can hold  
 // arraylist objects of type Integer  
  outer.add(innerInt1); // here innerInt1 is ArrayList Object 
  outer.add(innerInt2); // here innerInt2 is ArrayList Object 
  // Print using prinln function 
  System.out.println(outer); 
  // Pring using for loop dynamically 
  for(int i=0;i
  for(int j=0;j
  System.out.println(outer.get(i).get(j)); 
  } 
  } 
  } 
 } 
Salida :
[[100, 300, 500], [200, 400, 600]]
100300500200400600
APROBADO: arrayListTest == ============================================= Prueba predeterminada Las pruebas se ejecutan: 1, fallos: 0, saltos: 0 ======================================= ==== Conjunto predeterminado Total de pruebas ejecutadas: 1, Fallos: 0, Saltos: 0 =============================== ================
Respuesta
Podemos mostrar todos los elementos en ArrayList en Java usando:
- Por el uso de bucle for
- Por uso de bucle for mejorado
- Por uso de expresión lambda
- Por uso de referencia de método
- Mediante el uso de la secuencia
Veamos en el ejemplo:
 import java.util.ArrayList; 
 import java.util.Arrays; 
 public class MainClass  
 { 
  public static void main(String[] arg)  
  { 
  ArrayList 
   
  // Display all the elements in ArrayList in Java using for loop? 
  System.out.println("By use of for loop"); 
  for(int i = 0; i < list.size(); i++) 
  { 
  System.out.println(list.get(i)); 
  } 
   
  // Display all the elements in ArrayList in Java using enhanced for loop? 
  System.out.println("By use of enhanced for loop"); 
  for(Integer ele : list) 
  { 
  System.out.println(ele); 
  } 
   
  // Display all the elements in ArrayList in Java using forEach loop 
  // Using lambda expression  
  System.out.println("By use of lambda expression"); 
  list.forEach(a -> System.out.println(a)); 
   
  // Display all the elements in ArrayList in Java using forEach loop 
  // Using method reference 
  System.out.println("By use of method reference"); 
  list.forEach(System.out::println); 
   
  // Display all the elements in ArrayList in Java using forEach loop with Streams 
  System.out.println("By use of stream"); 
  list.stream().forEach(ele -> System.out.println(ele)); 
   
  } 
 }