for(변수 유형: 표 | collection){
.
.
}
for(String s : listString) {
System.out.println(s);
}
public class 루프 {출력:
public static void main(String[] args) {
String[] tstring = {"aa","ab","ac","ad","ae"};
for(문자열 s: tstring)
System.out.println(s);
}
}
aa
ab
ac
ad
ae
public class Parcours_List_foreach {런타임:
public static void main( String[] 인수) {
ArrayListarraylist = new ArrayList ();
arraylist.add("e1");
arraylist.add("e2");
arraylist.add("e3");
arraylist.add("e4");
for(문자열 s : arraylist){
System.out.println(들);
}
}
}
e1
e2
e3
e4
참고: 예를 들어 foreach를 사용하여 항목을 삭제하면 예외가 발생할 수 있는 경우와 같이 Iterator를 직접 사용해야 하는 상황이 있습니다. ConcurrentModificationException. |
반복자<정수> 반복자 = list.iterator();참조:
while(반복자.hasNext()) {
System.out.println(반복자.다음());
}
Please disable your ad blocker and refresh the window to use this website.