Java์ For Each Loop๋ ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์
์ ๊ฐ๋จํ๊ณ ํจ์จ์ ์ผ๋ก ์ํํ ์ ์๋ ๋ฐ๋ณต๋ฌธ์
๋๋ค. ์ผ๋ฐ์ ์ธ for
๋ฃจํ์ ๋นํด ์ฝ๋๊ฐ ๊ฐ๊ฒฐํ๊ณ ๊ฐ๋
์ฑ์ด ์ข์ ์์ฃผ ์ฌ์ฉ๋ฉ๋๋ค. ์ด๋ฒ ๊ธ์์๋ For Each Loop์ ๋ฌธ๋ฒ๊ณผ ํ์ฉ๋ฒ, ๊ทธ๋ฆฌ๊ณ ์ฃผ์์ฌํญ์ ๋ค์ํ ์์ ์ ํจ๊ป ์ดํด๋ณด๊ฒ ์ต๋๋ค.
1. For Each Loop ๊ธฐ๋ณธ ๋ฌธ๋ฒ
For Each Loop๋ ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์ ์ ์์๋ฅผ ํ๋์ฉ ์ํํ๋ฉฐ, ๊ฐ ์์์ ์ ๊ทผํ ์ ์๋๋ก ํฉ๋๋ค. ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
for (๋ฐ์ดํฐํ์
๋ณ์ : ๋ฐฐ์ด ๋๋ ์ปฌ๋ ์
) {
// ์คํํ ์ฝ๋
}
์ ๊ตฌ์กฐ์์ ๋ฐฐ์ด ๋๋ ์ปฌ๋ ์ ์ ๊ฐ ์์๊ฐ ๋ณ์์ ํ ๋น๋๋ฉฐ, ์ด๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
์์ : ๋ฐฐ์ด ์ํ
public class ForEachExample {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println("์ซ์: " + num);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์ซ์: 1
์ซ์: 2
์ซ์: 3
์ซ์: 4
์ซ์: 5
2. For Each Loop์ ๋ค์ํ ํ์ฉ
2.1 ๋ฌธ์์ด ๋ฐฐ์ด ์ํ
For Each Loop๋ ๋ฌธ์์ด ๋ฐฐ์ด์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
public class StringArrayExample {
public static void main(String[] args) {
String[] fruits = {"์ฌ๊ณผ", "๋ฐ๋๋", "์ฒด๋ฆฌ"};
for (String fruit : fruits) {
System.out.println("๊ณผ์ผ: " + fruit);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
๊ณผ์ผ: ์ฌ๊ณผ
๊ณผ์ผ: ๋ฐ๋๋
๊ณผ์ผ: ์ฒด๋ฆฌ
2.2 ์ปฌ๋ ์ ์ํ
For Each Loop๋ ArrayList
์ ๊ฐ์ ์ปฌ๋ ์
์ ์ํํ ๋๋ ์ ์ฉํฉ๋๋ค.
import java.util.ArrayList;
public class CollectionExample {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<>();
cars.add("BMW");
cars.add("Tesla");
cars.add("Audi");
for (String car : cars) {
System.out.println("์๋์ฐจ: " + car);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
์๋์ฐจ: BMW
์๋์ฐจ: Tesla
์๋์ฐจ: Audi
2.3 2์ฐจ์ ๋ฐฐ์ด ์ํ
For Each Loop๋ ์ค์ฒฉํ์ฌ 2์ฐจ์ ๋ฐฐ์ด์ ์ํํ๋ ๋ฐ๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
public class TwoDimArrayExample {
public static void main(String[] args) {
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
for (int[] row : matrix) {
for (int num : row) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
1 2 3
4 5 6
7 8 9
3. For Each Loop ์ฌ์ฉ ์ ์ฃผ์์ฌํญ
- ์์ ๋ถ๊ฐ: For Each Loop๋ ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์
์ ์์๋ฅผ ์ง์ ์์ ํ ์ ์์ต๋๋ค. ์์๋ฅผ ๋ณ๊ฒฝํ๋ ค๋ฉด
for
๋ฃจํ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. - ์ธ๋ฑ์ค ์ ๊ทผ ๋ถ๊ฐ: For Each Loop์์๋ ๋ฐฐ์ด์ ์ธ๋ฑ์ค์ ์ง์ ์ ๊ทผํ ์ ์์ผ๋ฏ๋ก, ์ธ๋ฑ์ค๊ฐ ํ์ํ ์์
์์๋
for
๋ฃจํ๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. - NullPointerException: ์ํํ๋ ค๋ ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์
์ด
null
์ด๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ฏ๋ก, ๋ฐ๋์ null ๊ฒ์ฌ๋ฅผ ์ํํ์ธ์.
์์ ์์ ์ด ํ์ํ ๊ฒฝ์ฐ
public class ModifyArray {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
// ๊ธฐ์กด ์์ ๊ฐ์ 2๋ฐฐ๋ก ๋ณ๊ฒฝ
for (int i = 0; i < numbers.length; i++) {
numbers[i] *= 2;
}
for (int num : numbers) {
System.out.println(num);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
2
4
6
8
10
4. For Each Loop์ ๊ธฐ์กด For Loop ๋น๊ต
For Each Loop๋ ๊ฐ๊ฒฐํ์ง๋ง, ๊ธฐ์กด for
๋ฃจํ์ ๋นํด ์ ํ์ด ์์ต๋๋ค. ์๋๋ ๋ ๋ฐฉ์์ ๋น๊ต ์์ ์
๋๋ค.
For Each Loop
int[] numbers = {10, 20, 30};
for (int num : numbers) {
System.out.println(num);
}
๊ธฐ์กด For Loop
int[] numbers = {10, 20, 30};
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
For Each Loop๋ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ํ์ง๋ง, ์์๋ฅผ ์์ ํ๊ฑฐ๋ ์ธ๋ฑ์ค๋ฅผ ์ฐธ์กฐํ ํ์๊ฐ ์๋ ๊ฒฝ์ฐ ๊ธฐ์กด for
๋ฃจํ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ๋ ์ ํฉํฉ๋๋ค.
5. ๊ฒฐ๋ก
Java์ For Each Loop๋ ๋ฐฐ์ด๊ณผ ์ปฌ๋ ์
์ ๊ฐ๋จํ ์ํํ ์ ์์ด ์ฝ๋์ ๊ฐ๋
์ฑ์ ๋์ด๊ณ ์ค๋ฅ๋ฅผ ์ค์ด๋ ๋ฐ ์ ์ฉํฉ๋๋ค. ๋ค์ํ ์์ ๋ฅผ ํตํด For Each Loop์ ํ์ฉ๋ฒ์ ์ตํ๊ณ , ํ์์ ๋ฐ๋ผ ๊ธฐ์กด for
๋ฃจํ์ ์ ์ ํ ์กฐํฉํ์ฌ ์ฌ์ฉํด ๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ