如何为每个循环更改for循环

时间:2014-12-17 03:22:35

标签: java for-loop foreach

CODE:

for (int i =0; i<=10; i++) {
System.out.println(movieasArrayList.get(i).getID() + " " + movieasArrayList.get(i).getAction()); 
  }

问题:

将此for-loop更改为for-each loop,以便为所有电影(不仅仅是前11位)执行相同的任务。

任何帮助将不胜感激!!

1 个答案:

答案 0 :(得分:1)

java中的for-each循环结构如下:

// works with collections and arrays, everything
// that has an iterator
Collection<Object> c = new Collection<Object>();
for (Object i : c) {
    // do code
}

对你来说,这看起来像这样:

// I don't know what your movieArrayList contains
for (Movie i : movieArrayList) {
    System.out.println(i.getId() + " " +  i.getAction());
}

希望这有帮助!