๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/โ˜• Java

Java | Foreach

  • -

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

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.