Print an ArrayList with a for-each loop
288,592
Solution 1
Your code works. If you don't have any output, you may have "forgotten" to add some values to the list:
// add values
list.add("one");
list.add("two");
// your code
for (String object: list) {
System.out.println(object);
}
Solution 2
import java.util.ArrayList;
import java.util.List;
class ArrLst{
public static void main(String args[]){
List l=new ArrayList();
l.add(10);
l.add(11);
l.add(12);
l.add(13);
l.add(14);
l.forEach((a)->System.out.println(a));
}
}
Author by
Jordan Westlund
Updated on April 07, 2020Comments
-
Jordan Westlund about 4 years
Given the following exists in a class, how do I write a for-each that prints each item in the list?
private ArrayList<String> list; list = new ArrayList<String>();
I have:
for (String object: list) { System.out.println(object); }