Java 1.5 version ์ด์๋ถํฐ ์ถ๊ฐ๋ Foreach ๋ฃจํ๊ฐ ์ถ๊ฐ๋์๋ค.
์ค์ ๋ช
๋ น์ด๋ foreach๊ฐ ์๋๋ผ ๊ธฐ์กด for๋ฅผ ์ฌ์ฉํ๋ค.
foreach ๋ ํ์ดํ ์๋ ์ ๊ณ ๊ฐ๋
์ฑ๋ ์ข์์ ํธ๋ฆฌํ๋ค.
ํํ
for (type var : iterate) {
body-of-loop
}
์ฌ์ฉ๋ฐฉ๋ฒ
ํ์์ ์ฌ์ฉํ๋ for๋ฌธ์ ํํ
String[] temp = { "aa", "bb", "cc" };
for (int i = 0; i < temp .length; i++) {
System.out.println(temp[i]);
}
foreachํํ
String[] temp = { "aa", "bb", "cc" };
for (String el : temp) {
System.out.println(el);
}
foreachํํ(List)
List<VO> indexList = { List1, List2, List3 }; // VO ํํ์ ๋ฆฌ์คํธ
for (VO vo : indexList) { // 1์คํญ์ฉ ์์ฐจ์ ์ผ๋ก ๋ฐ๋ณตํ ๊ฐ์ ๊ฐ์ ธ์จ๋ค.
System.out.println(vo.getName);
System.out.println(vo.getAge);
System.out.println(vo.getAddr);
System.out.println(vo.getDesc);
...
}
์ถ์ฒ | https://blog.outsider.ne.kr/271