Java์์ ๋ฐฐ์ด(Array)์ ๋์ผํ ๋ฐ์ดํฐ ํ์ ์ ๊ฐ์ ์ ์ฅํ๋ ๊ตฌ์กฐ๋ก, ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ์์๋ฅผ ํจ์จ์ ์ผ๋ก ์ํํ ์ ์์ต๋๋ค. ๋ฐฐ์ด ์ํ๋ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ฑฐ๋ ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ์์๋ฅผ ์ฐพ๋ ๋ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค. ์ด๋ฒ ๊ธ์์๋ for loop, for-each loop, while loop ๋ฑ์ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ์ํํ๋ ๋ฐฉ๋ฒ๊ณผ ํ์ฉ ์์ ๋ฅผ ์๊ฐํ๊ฒ ์ต๋๋ค.
1. For Loop๋ก ๋ฐฐ์ด ์ํํ๊ธฐ
For Loop๋ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ ์์์ ์ ๊ทผํ ์ ์๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ์ ๋๋ค.
์์ : For Loop๋ก ๋ฐฐ์ด ์ํ
public class ForLoopExample {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
for (int i = 0; i < numbers.length; i++) {
System.out.println("Index " + i + ": " + numbers[i]);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Index 4: 50
For Loop๋ ๋ฐฐ์ด์ ๊ธธ์ด๋งํผ ๋ฐ๋ณต๋๋ฉฐ, ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํด ๋ฐฐ์ด ์์์ ์ ๊ทผํฉ๋๋ค.
2. For-Each Loop๋ก ๋ฐฐ์ด ์ํํ๊ธฐ
For-Each Loop๋ ๋ฐฐ์ด์ ์์๋ฅผ ํ๋์ฉ ์ํํ๋ฉฐ, ์ฝ๋๊ฐ ๊ฐ๊ฒฐํด์ง๊ณ ๊ฐ๋ ์ฑ์ด ์ข์์ง๋๋ค.
์์ : For-Each Loop๋ก ๋ฐฐ์ด ์ํ
public class ForEachExample {
public static void main(String[] args) {
String[] fruits = {"Apple", "Banana", "Cherry"};
for (String fruit : fruits) {
System.out.println("Fruit: " + fruit);
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Fruit: Apple
Fruit: Banana
Fruit: Cherry
For-Each Loop๋ ๋ฐฐ์ด์ ๊ฐ ์์์ ์๋์ผ๋ก ์ ๊ทผํ๋ฉฐ, ์ธ๋ฑ์ค๋ฅผ ์ง์ ์ฌ์ฉํ์ง ์์๋ ๋ฉ๋๋ค.
3. While Loop๋ก ๋ฐฐ์ด ์ํํ๊ธฐ
While Loop๋ ์กฐ๊ฑด์ด ์ฐธ์ผ ๋์ ๋ฐ๋ณต๋๋ฏ๋ก, ๋ฐฐ์ด ์ํ์๋ ํ์ฉํ ์ ์์ต๋๋ค.
์์ : While Loop๋ก ๋ฐฐ์ด ์ํ
public class WhileLoopExample {
public static void main(String[] args) {
int[] numbers = {5, 10, 15, 20};
int i = 0;
while (i < numbers.length) {
System.out.println("Number: " + numbers[i]);
i++;
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Number: 5
Number: 10
Number: 15
Number: 20
While Loop๋ ๋ฃจํ ๋ณ์๋ฅผ ๋ช ์์ ์ผ๋ก ๊ด๋ฆฌํด์ผ ํ๋ฏ๋ก, For Loop์ ๋นํด ์ฝ๋๊ฐ ๋ค์ ๊ธธ์ด์ง ์ ์์ต๋๋ค.
4. ๋ฐฐ์ด ์ํ์ ํ์ฉ ์์
4.1 ๋ฐฐ์ด ์์์ ํฉ๊ณ ๊ตฌํ๊ธฐ
public class ArraySum {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
int sum = 0;
for (int number : numbers) {
sum += number;
}
System.out.println("Sum of array elements: " + sum);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Sum of array elements: 15
์ด ์ฝ๋๋ For-Each Loop๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ํฉ์ฐํฉ๋๋ค.
4.2 ๋ฐฐ์ด ์์ ์ค ์ต๋๊ฐ ์ฐพ๊ธฐ
public class ArrayMax {
public static void main(String[] args) {
int[] numbers = {7, 3, 9, 2, 5};
int max = numbers[0];
for (int number : numbers) {
if (number > max) {
max = number;
}
}
System.out.println("Maximum value: " + max);
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Maximum value: 9
๋ฐฐ์ด์ ์์๋ฅผ ์ํํ๋ฉฐ ์ต๋๊ฐ์ ์ฐพ๋ ์์ ์ ๋๋ค.
4.3 ํน์ ๊ฐ์ ์ธ๋ฑ์ค ์ฐพ๊ธฐ
public class FindIndex {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int target = 30;
int index = -1;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == target) {
index = i;
break;
}
}
if (index != -1) {
System.out.println("Target found at index: " + index);
} else {
System.out.println("Target not found.");
}
}
}
์ถ๋ ฅ ๊ฒฐ๊ณผ:
Target found at index: 2
์ด ์ฝ๋๋ ํน์ ๊ฐ์ ์ธ๋ฑ์ค๋ฅผ ์ฐพ์ ์ถ๋ ฅํฉ๋๋ค.
5. ๋ฐฐ์ด ์ํ ์ ์ฃผ์์ฌํญ
- ์ธ๋ฑ์ค ๋ฒ์: ๋ฐฐ์ด์ ์ ํจํ ์ธ๋ฑ์ค ๋ฒ์๋ 0๋ถํฐ
length - 1
๊น์ง์ ๋๋ค. ์ด๋ฅผ ๋ฒ์ด๋๋ฉดArrayIndexOutOfBoundsException
์ด ๋ฐ์ํฉ๋๋ค. - Null ๋ฐฐ์ด ํ์ธ: ๋ฐฐ์ด์ด
null
์ผ ๊ฒฝ์ฐ ์ํ ์ ์ ๋ฐ๋์ null ๊ฒ์ฌ๋ฅผ ํด์ผ ํฉ๋๋ค.
๊ฒฐ๋ก
Java์์ ๋ฐฐ์ด ์ํ๋ ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฅผ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ๋ ํต์ฌ ๊ธฐ์ ์ ๋๋ค. For Loop, For-Each Loop, While Loop ๋ฑ ๋ค์ํ ๋ฐฉ์์ผ๋ก ๋ฐฐ์ด์ ์ํํ ์ ์์ผ๋ฉฐ, ๊ฐ ๋ฐฉ๋ฒ์ ์ฌ์ฉ ๋ชฉ์ ์ ๋ฐ๋ผ ์ ํํ ์ ์์ต๋๋ค. ์ด๋ฒ ๊ธ์์ ์๊ฐํ ๋ค์ํ ์์ ๋ฅผ ํตํด ๋ฐฐ์ด ์ํ์ ๊ธฐ์ด์ ํ์ฉ๋ฒ์ ์ตํ๊ณ , ์ค์ ์์ ํ์ฉํด ๋ณด์ธ์!
๋๊ธ ์ฐ๊ธฐ