Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print an ArrayList with a for-each loop

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); } 
like image 273
Jordan Westlund Avatar asked Mar 21 '12 21:03

Jordan Westlund


1 Answers

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); } 
like image 58
Andreas Dolk Avatar answered Sep 17 '22 22:09

Andreas Dolk