์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- Absolute
- AGI
- ai
- AI agents
- AI engineer
- AI researcher
- ajax
- algorithm
- Algorithms
- aliases
- Array ๊ฐ์ฒด
- ASI
- bayes' theorem
- Bit
- Blur
- BOM
- bootstrap
- canva
- challenges
- ChatGPT
- Today
- In Total
A Joyful AI Research Journey๐ณ๐
[28] 230207 Java Ch. 5 ์ฐธ์กฐ ํ์ : 2. ๋ฐฐ์ด [K-๋์งํธ ํธ๋ ์ด๋ 28์ผ] ๋ณธ๋ฌธ
[28] 230207 Java Ch. 5 ์ฐธ์กฐ ํ์ : 2. ๋ฐฐ์ด [K-๋์งํธ ํธ๋ ์ด๋ 28์ผ]
yjyuwisely 2023. 2. 7. 14:44230207 Tue 28th class
Ch. 5 ์ฐธ์กฐ ํ์
์ง๋: p. 186 ~ (๊ต์ฌ: ํผ์ ๊ณต๋ถํ๋ ์๋ฐ, ์ ์: ์ ์ฉ๊ถ)
ํผ์ ๊ณต๋ถํ๋ ์๋ฐ
๋ ํ์ผ๋ก ์๋ฐ๋ฅผ ๋ฐฐ์ฐ๋ ์ ๋ฌธ์๊ฐ โ๊ผญ ํ์ํ ๋ด์ฉ์ ์ ๋๋กโ ํ์ตํ ์ ์๋๋ก ๊ตฌ์ฑํ๋ค. โ๋ฌด์์โ โ์ด๋ป๊ฒโ ํ์ตํด์ผ ํ ์ง ์กฐ์ฐจ ๋ชจ๋ฅด๋ ์ ๋ฌธ์์ ๋ง์ฐํ ๋ง์์ ์ดํด, ๊ณผ์ธ ์ ์๋์ด
www.aladin.co.kr
์ค๋ ๋ฐฐ์ด ๊ฒ ์ค ๊ธฐ์ตํ ๊ฒ์ ์ ๋ฆฌํ๋ค.
์ง๋ ์์ ๋ ๋ฐฐ์ด ๊ฒ ์ค ๋ค์ ๊ธฐ์ตํ ๊ฒ
System.out.println(); ์ํฐ ํจ๊ณผ๊ฐ ์๋ค.
Continue: (//์คํต) break๋ฌธ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก continue๋ฌธ๋ ๋๊ฐ if๋ฌธ๊ณผ ๊ฐ์ด ์ฌ์ฉ๋๋๋ฐ,
ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฒฝ์ฐ์ continue๋ฌธ์ ์คํํด์ ๊ทธ ์ดํ์ ๋ฌธ์ฅ์ ์คํํ์ง ์๊ณ ๋ค์ ๋ฐ๋ณต์ผ๋ก ๋์ด๊ฐ๋ค.
boolean ํ์ ์ ๋ฐฐ์ด ํญ๋ชฉ์ ๊ธฐ๋ณธ ์ด๊ธฐ๊ฐ์ false์ด๋ค.
Ch. 5 ์๋ฐ ์ฐธ์กฐ ํ์
์ฑ ์ ๋ชฉ์ฐจ
05-1 ์ฐธ์กฐ ํ์ ๊ณผ ์ฐธ์กฐ ๋ณ์
05-2 ๋ฐฐ์ด
05-3 ์ด๊ฑฐ ํ์
5.2 ๋ฐฐ์ด p. 177
1) ๋ฐฐ์ด์ด๋? p. 178
๋ฐฐ์ด์ ๊ฐ์ ํ์
์ ๋ฐ์ดํฐ๋ฅผ ์ฐ์๋ ๊ณต๊ฐ์ ๋์ดํ๊ณ ๊ฐ ๋ฐ์ดํฐ์ ์ธ๋ฑ์ค(index)๋ฅผ ๋ถ์ฌํด ๋์ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
- ๋ฐฐ์ด์ ๊ฐ์ ํ์
์ ๋ฐ์ดํฐ๋ง ์ ์ฅํ ์ ์๋ค.
- ํ ๋ฒ ์์ฑ๋ ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ ๋๋ฆฌ๊ฑฐ๋ ์ค์ผ ์ ์๋ค.
2) ๋ฐฐ์ด ์ ์ธ p. 179
๋ณ์ ์ ์ธ int 10; int 20; \์ด๊ธฐํ a=100; b=20; ๋ฐฐ์ด ์ ์ธ int[] score//ํ์
[] ๋ณ์๋ช
int score[]//ํ์
๋ณ์๋ช
[]
๋ ๊ฐ์ง ํ์์ผ๋ก ์์ฑํ ์ ์๋ค.
ํ์
[] ๋ณ์;
ํ์
๋ณ์[];
3) ๋ฐฐ์ด ์์ฑ p. 180
2.1) ๊ฐ ๋ชฉ๋ก์ผ๋ก ๋ฐฐ์ด ์์ฑ
ํ์
[] ๋ณ์ = {๊ฐ0, ๊ฐ1, ๊ฐ2, ๊ฐ3, ...}; String[] names = {"์ ์ฉ๊ถ", "ํ๊ธธ๋", "๊น์๋ฐ"};
{๊ฐ1, ๊ฐ2, ๊ฐ3, ... }์ฒ๋ผ ๊ฐ ๋ชฉ๋ก์ผ๋ก ์์ฑํ๋ค.
์์ฑ๊ณผ ์ด๊ธฐํ ๋์์
์์ ) ๊ฐ ๋ชฉ๋ก์ผ๋ก ๋ฐฐ์ด ์์ฑ
public class P180ArrayCreateByValueListExample { public static void main(String[] args) { int[] scores = {83, 90, 87}; System.out.println("scores[0] : "+scores[0]); System.out.println("scores[1] : "+scores[1]); System.out.println("scores[2] : "+scores[2]); int sum = 0; for(int i=0; i<3; i++) { sum += scores[i]; } System.out.println("์ดํฉ : "+sum); double avg = (double) sum/3; System.out.println("ํ๊ท : "+avg); } }
๊ฒฐ๊ณผ)
scores[0] : 83 scores[1] : 90 scores[2] : 87 ์ดํฉ : 260 ํ๊ท : 86.66666666666667
๋ฐฐ์ด ๋ณ์๋ฅผ ์ด๋ฏธ ์ ์ธํ ํ์๋ ๋ค๋ฅธ ์คํ๋ฌธ์์ ์ค๊ดํธ๋ฅผ ์ฌ์ฉํ ๋ฐฐ์ด ์์ฑ์ด ํ์ฉ๋์ง ์๋๋ค.
ํ์
[] ๋ณ์; int[] scores; ๋ณ์ = {๊ฐ0, ๊ฐ1, ๊ฐ2, ๊ฐ3, ...} //์ปดํ์ผ ์๋ฌ
๋ฐฐ์ด ๋ณ์๋ฅผ ๋ฏธ๋ฆฌ ์ ์ธํ ํ ๊ฐ ๋ชฉ๋ก๋ค์ด ๋์ค์ ๊ฒฐ์ ๋๋ ์ํฉ์ด๋ผ๋ฉด new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ ๊ฐ ๋ชฉ๋ก์ ์ง์ ํด ์ฃผ๋ฉด ๋๋ค.
new ์ฐ์ฐ์ ๋ฐ๋ก ๋ค์๋ ๋ฐฐ์ด ๋ณ์ ์ ์ธ์์ ์ฌ์ฉํ "ํ์
[]"์ ๋ถ์ฌ์ฃผ๊ณ ์ค๊ดํธ {}์๋ ๊ฐ๋ค์ ๋์ดํ๋ค.
๋ณ์ = new ํ์
[] {๊ฐ0, ๊ฐ1, ๊ฐ2, ๊ฐ3, ...}; scores = new int[] {83, 90, 87};
String[] names = null; names = new String[] {"์ ์ฉ๊ถ", "ํ๊ธธ๋", "๊น์๋ฐ"} int[] scores; scores = new int[]{83, 90, 87};
๋ฉ์๋์ ๋งค๊ฐ๊ฐ์ด ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ๋ ๋ง์ฐฌ๊ฐ์ง์ด๋ค.
๋งค๊ฐ ๋ณ์๋ก int[] ๋ฐฐ์ด์ด ์ ์ธ๋ add ๋ฉ์๋๊ฐ ์์ ๊ฒฝ์ฐ, ๊ฐ ๋ชฉ๋ก์ผ๋ก ๋ฐฐ์ด์ ์์ฑํจ๊ณผ ๋์์ add() ๋ฉ์๋์ ๋งค๊ฐ๊ฐ์ผ๋ก ์ฌ์ฉํ๊ณ ์ ํ ๋๋ ๋ฐ๋์ new ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
int add(int[] scores) {...} int result = add({95, 85, 90});//์ปดํ์ผ ์๋ฌ int result = add(new int[] {95, 85, 90});
์์ )
public class P182ArrayCreatByValueListExample2 { public static void main(String[] args) { int[] scores; //scores๋ผ๋ ๋ฐฐ์ด์ ์ ์ธํ๋ค. scores = new int[] {83, 90, 87}; int sum1 = 0; for (int i=0; i<3; i++) { sum1 += scores[i]; } System.out.println("์ดํฉ: " + sum1); int sum2 = add(new int[] {83, 90, 87}); System.out.println("์ดํฉ: " + sum2); System.out.println(); } //์ดํฉ์ ๊ณ์ฐํด์ ๋ฆฌํดํ๋ ๋ฉ์๋์ด๋ค. public static int add(int[] scores) { int sum = 0; for (int i=0; i<3; i++) { sum += scores[i]; } return sum; } }
๊ฒฐ๊ณผ)
์ดํฉ: 260 ์ดํฉ: 260
2.2) new ์ฐ์ฐ์๋ก ๋ฐฐ์ด์์ฑ
๊ฐ์ ๋ชฉ๋ก์ ๊ฐ์ง๊ณ ์์ง ์์ง๋ง, ํฅํ ๊ฐ๋ค์ ์ ์ฅํ ๋ฐฐ์ด์ ๋ฏธ๋ฆฌ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด new ์ฐ์ฐ์๋ก ๋ฐฐ์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
ํ์
[] ๋ณ์ = new ํ์
[๊ธธ์ด];
๊ธธ์ด๋ ๋ฐฐ์ด์ด ์ ์ฅํ ์ ์๋ ๊ฐ์ด ๊ฐ์์ด๋ค. ์ด๋ฏธ ๋ฐฐ์ด ๋ณ์๊ฐ ์ ์ธ๋ ๊ฒฝ์ฐ์๋ new ์ฐ์ฐ์๋ก ๋ฐฐ์ด์ ์์ฑํ ์ ์๋ค.
ํ์
[] ๋ณ์ = null; ๋ณ์ = new ํ์
[๊ธธ์ด];
๊ธธ์ด๊ฐ 5์ธ int[] ๋ฐฐ์ด์ ์์ฑํ๋ค.
int[] intArray = new int[5];
์์ฑ๊ณผ ์ด๊ธฐํ ๋ฐ๋ก, ์์ฑ๊ณผ ๋์์ ์ด๊ธฐํ
๋ฐฐ์ด scores์ 0,1,2 ์ธ๋ฑ์ค์ ๊ฐ๊ฐ 83, 90, 75๋ฅผ ์ ์ฅํ๋ ์ฝ๋์ด๋ค.
int[] scores = new int[3]; scores[0] = 83; scores[1] = 90; scores[2] = 75;
0x100 | ||||
arr1 | 0x100<--- | 0 | 0 | 0 |
arr[0] | arr[1] | arr[2] |
์์ )
public class ArraryCreatByNewExample { public static void main(String[] args) { int[] arr1 = new int[3]; //3๊ฐ์ ๊ฐ์ด ์๋ค. for(int i=0;i<3;i++) { System.out.println("arr1[" + i + "]: " + arr1[i]); } //0 0 0 arr1[0] = 10; arr1[1] = 20; arr1[2] = 30; for(int i=0; i<3; i++) { System.out.println("arr1[" + i + "]: " + arr1[i]); } //10 20 30 double[] arr2 = new double[3]; for(int i=0; i<3; i++) { System.out.println("arr2[" + i + "]: " + arr2[i]); } //0.0 0.0 0.0 arr2[0] = 0.1; arr2[1] = 0.2; arr2[2] = 0.3; for(int i=0; i<3; i++) { System.out.println("arr2[" + i + "]: " + arr2[i]); } //0.1 0.2 0.3 String[] arr3 = new String[3]; for(int i=0; i<3; i++) { System.out.println("arr3[" + i + "]: " + arr3[i]); } //null null null arr3[0] = "1์"; arr3[1] = "2์"; arr3[2] = "3์"; for(int i=0; i<3; i++) { System.out.println("arr3[" + i + "]: " + arr3[i]); } //1์ 2์ 3์ } }
๊ฒฐ๊ณผ)
arr1[0]: 0 arr1[1]: 0 arr1[2]: 0 arr1[0]: 10 arr1[1]: 20 arr1[2]: 30 arr2[0]: 0.0 arr2[1]: 0.0 arr2[2]: 0.0 arr2[0]: 0.1 arr2[1]: 0.2 arr2[2]: 0.3 arr3[0]: null arr3[1]: null arr3[2]: null arr3[0]: 1์ arr3[1]: 2์ arr3[2]: 3์
4) ๋ฐฐ์ด ๊ธธ์ด p. 185
๋ฐฐ์ด์ ์ ์ฅ๋ ์ ์๋ ํญ๋ชฉ(๊ฐ)์ ์๋ฅผ ๋งํ๋ค. ๋ฐฐ์ด์ length ํ๋์ ์ ์ฅ๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ณ์.length๋ก ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ์ฝ์ ์ ์๋ค.
์์)
public class P186ArrayLengthTime { public static void main(String[] args) { int[] scores = {83, 90, 87}; int sum = 0; for(int i = 0; i<scores.length; i++) { sum += scores[i]; } System.out.println("์ดํฉ: " + sum); double avg = (double) sum/scores.length; System.out.println("ํ๊ท : " + avg); } }
๊ฒฐ๊ณผ)
์ดํฉ: 260 ํ๊ท : 86.66666666666667
๋ฌธ์ )
public class P187A { public static void main(String[] args) { /* 10, 20, 30, 50, 3, 60, -3 ๊ฐ์ ๋ฐฐ์ด์ ์ง์ ํ๊ณ ์๋ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์ธ์. * ๋ฌธ์ 1. arr ๋ฐฐ์ด ์ค์์ ์ธ๋ฑ์ค๊ฐ 1์ธ ๊ณณ์ ๊ฐ์ ์ถ๋ ฅํด๋ณด์. ์ ๋ต)20*/ int[] arr = {10,20,30,50,3,60,-3}; /*int[] arr = new int[7]; * arr[0]=10; * arr[1]=20; * arr[2]=30; * arr[4]=50; * arr[5]=3; * arr[6]=60; * arr[7]=-3; */ System.out.println(arr[1]); /*๋ฌธ์ 2. arr ๋ฐฐ์ด ์ค์์ ๊ฐ์ด 60์ธ ๊ณณ์ ์ธ๋ฑ์ค๋ฅผ ์ถ๋ ฅํด๋ณด์. ์ ๋ต)5*/ for (int i=0;i<7;i++) { //arr ๋ฐฐ์ด ๊ฐ ์ค์์ 60์ด๋ฉด if (arr[i]==60) { System.out.println(i); } } /*๋ฌธ์ 3. arr ๋ฐฐ์ด์ ์ธ๋ฑ์ค๊ฐ 3์ธ ๊ณณ์ ์ถ๋ ฅํ์ง ๋ง๊ณ ๋๋จธ์ง๋ง ์ถ๋ ฅํด๋ณด์.*/ for(int i=0; i<7; i++) { if (i!=3) { System.out.print(arr[i]+","); } } System.out.println(); /*๋ฌธ์ 3.(์์
) arr ๋ฐฐ์ด์ ์ธ๋ฑ์ค๊ฐ 3์ธ ๊ณณ์ ์ถ๋ ฅํ์ง ๋ง๊ณ ๋๋จธ์ง๋ง ์ถ๋ ฅํด๋ณด์.*/ for(int i=0; i<7; i++) { if (i==3) { continue; } System.out.print(arr[i]+","); } } }
์ ๋ต) if (i==3){
20 5 10,20,30,3,60,-3, 10,20,30,3,60,-3,
A1)
0x100 | |||||||||
arr | 0x100<--- | arr | 10 | 20 | 30 | 50 | 3 | 60 | -3 |
arr[0] | arr[1] | arr[2] | arr[3] | arr[4] | arr[5] | arr[6] |
์์ )
/* 10, 20, 30, 50, 3, 60, -3 ๊ฐ์ ๋ฐฐ์ด์ ์ง์ ํ๊ณ ์๋ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์ธ์. * ๋ฌธ์ 1. arr ๋ฐฐ์ด ์ค์์ ์ธ๋ฑ์ค๊ฐ 1์ธ ๊ณณ์ ๊ฐ์ ์ถ๋ ฅํด๋ณด์. ์ ๋ต)20 * ๋ฌธ์ 2. arr ๋ฐฐ์ด ์ค์์ ๊ฐ์ด 60์ธ ๊ณณ์ ์ธ๋ฑ์ค๋ฅผ ์ถ๋ ฅํด๋ณด์. ์ ๋ต)5 * ๋ฌธ์ 3. arr ๋ฐฐ์ด์ ์ธ๋ฑ์ค๊ฐ 3์ธ ๊ณณ์ ์ถ๋ ฅํ์ง ๋ง๊ณ ๋๋จธ์ง๋ง ์ถ๋ ฅํด๋ณด์. * * ๋ฐฐ์ด ๋ฌธ์ ์ ๋ณ๊ฐ๋ก ๊ตฌ๊ธ์ ์ต๋๊ฐ ์ต์๊ฐ์ ์ด๋ค ์์ผ๋ก ๊ตฌํ๋์ง ์์๋ณด์ธ์. */ public class P187A { public static void main(String[] args) { /* 10, 20, 30, 50, 3, 60, -3 ๊ฐ์ ๋ฐฐ์ด์ ์ง์ ํ๊ณ ์๋ ๋ฌธ์ ๋ฅผ ํ์ด๋ณด์ธ์. * ๋ฌธ์ 1. arr ๋ฐฐ์ด ์ค์์ ์ธ๋ฑ์ค๊ฐ 1์ธ ๊ณณ์ ๊ฐ์ ์ถ๋ ฅํด๋ณด์. ์ ๋ต)20*/ int[] arr = {10,20,30,50,3,60,-3}; /*int[] arr = new int[7]; * arr[0]=10; * arr[1]=20; * arr[2]=30; * arr[4]=50; * arr[5]=3; * arr[6]=60; * arr[7]=-3; */ System.out.println(arr[1]); /*๋ฌธ์ 2. arr ๋ฐฐ์ด ์ค์์ ๊ฐ์ด 60์ธ ๊ณณ์ ์ธ๋ฑ์ค๋ฅผ ์ถ๋ ฅํด๋ณด์. ์ ๋ต)5*/ for (int i=0;i<7;i++) { //arr ๋ฐฐ์ด ๊ฐ ์ค์์ 60์ด๋ฉด if (arr[i]==60) { System.out.println(i); } } /*๋ฌธ์ 3. arr ๋ฐฐ์ด์ ์ธ๋ฑ์ค๊ฐ 3์ธ ๊ณณ์ ์ถ๋ ฅํ์ง ๋ง๊ณ ๋๋จธ์ง๋ง ์ถ๋ ฅํด๋ณด์.*/ for(int i=0; i<7; i++) { if (i!=3) { System.out.print(arr[i]+","); } } /*๋ฌธ์ 3.(์์
์ค) arr ๋ฐฐ์ด์ ์ธ๋ฑ์ค๊ฐ 3์ธ ๊ณณ์ ์ถ๋ ฅํ์ง ๋ง๊ณ ๋๋จธ์ง๋ง ์ถ๋ ฅํด๋ณด์.*/ for(int i=0; i<7; i++) { if (i==3) { continue;//์คํต } System.out.print(arr[i]+","); } } }
๊ฒฐ๊ณผ) if (i==3){continue;}๋ก๋ ๊ฐ๋ฅํ๋ค.
์ฐธ๊ณ : https://dpdpwl.tistory.com/95
20 5 10,20,30,3,60,-3,10,20,30,3,60,-3,
5) ๋ช
๋ น ๋ผ์ธ ์
๋ ฅ p. 186
์์ ) main() ๋ฉ์๋์ ๋งค๊ฐ ๋ณ์
์คํํ ๋ 2๊ฐ์ ๋ฌธ์์ด์ ์ฃผ์ง ์์ผ๋ฉด "๊ฐ์ ์๊ฐ ๋ถ์กฑํฉ๋๋ค."๋ฅผ ์ถ๋ ฅํ๊ณ ๊ฐ์ ์ข
๋ฃํ๋ค.
๋ง์ฝ 2๊ฐ์ ๋ฌธ์์ด์ด ์ ํํ๊ฒ ์
๋ ฅ๋์๋ค๋ฉด 2๊ฐ์ ๋ฌธ์์ด์ ์ ์๋ก ๋ณํํ๊ณ ๋ง์
์ฐ์ฐ์ ์ํํ๋ค.
0x100 | "20"-> 20 | ||
args<--- | args | args[0] | args[1] |
public class P167MainStringArrayArgument { public static void main(String[] args) { if(args.length != 2) { //์
๋ ฅ๋ ๋ฐ์ดํฐ์ ์๊ฐ 2๊ฐ๊ฐ ์๋ ๊ฒฝ์ฐ System.out.println("๊ฐ์ ์๊ฐ ๋ถ์กฑํฉ๋๋ค."); System.exit(0); //ํ๋ก๊ทธ๋จ ๊ฐ์ ์ข
๋ฃ } String strNum1 = args[0]; //์ฒซ ๋ฒ์งธ ๋ฐ์ดํฐ ์ป๊ธฐ String strNum2 = args[1]; //๋ ๋ฒ์งธ ๋ฐ์ดํฐ ์ป๊ธฐ int num1 = Integer.parseInt(strNum1); //๋ฌธ์์ด์ ์ ์๋ก ๋ณํ int num2 = Integer.parseInt(strNum2); //๋ฌธ์์ด์ ์ ์๋ก ๋ณํ int result = num1 + num2; System.out.println(num1 + "+" + num2 + "=" + result); } }โ
๊ฒฐ๊ณผ) Run - Run configurations - Arguments์์ 10, 20์ ์
๋ ฅํ๋ค.
strNum1 | "10" | num1 | 10 |
strNum2 | "20" | num2 | 20 |
num3 | 30 |
10+20=30
6) ๋ค์ฐจ์ ๋ฐฐ์ด p. 190
๋ฐฐ์ด ํญ๋ชฉ(๊ฐ)์ด ๋ ๋ค๋ฅธ ๋ฐฐ์ด์ ์ฐธ์กฐํ ๋ ์ด๊ฒ์ ๋ค์ฐจ์ ๋ฐฐ์ด์ด๋ผ๊ณ ํ๋ค.
๋ค์ฐจ์ ๋ฐฐ์ด์ ์ ์ธ๊ณผ ์์ฑ ๋ฐฉ๋ฒ์ ํ์
[] [] ๋ณ์ = new ํ์
[๊ธธ์ด1][๊ธธ์ด2];์ด๋ค. (ํ(์ค), ์ด(์นธ))
๋ณ์.length๋ 1์ฐจ์ ๋ฐฐ์ด์ ๊ธธ์ด1์ด ๋๊ณ ,
๋ณ์[์ธ๋ฑ์ค].length๋ ํด๋น ํญ๋ชฉ์ด ์ฐธ์กฐํ๋ 2์ฐจ์ ๋ฐฐ์ด์ ๊ธธ์ด2๋ฅผ ๋งํ๋ค.
int[] score = new int[5] //1์ฐจ์ ๋ฐฐ์ด int score[] = new int[5] //1์ฐจ์ ๋ฐฐ์ด score[0]=10; score[1]=20; score[2]=30; score[3]=40; score[4]=50; int score[] = {10,20,30,40,50} int[][] score = new int[5][3] //2์ฐจ์ ๋ฐฐ์ด (5ํ, 3์ด) int score[][] = new int[3][3] //2์ฐจ์ ๋ฐฐ์ด int[] score[] = new int[5][3] //2์ฐจ์ ๋ฐฐ์ด score2[0][0]=10; score2[0][1]=20; score2[0][2]=30; score2[1][0]=40; score2[1][1]=50; score2[1][2]=60; score2[2][0]=70; score2[2][1]=80; score2[2][2]=90; score2[3][0]=100; score2[3][1]=110; score2[3][2]=120; score2[4][0]=130; score2[4][1]=140; score2[4][2]=150; int score2[][] = { {10,20,30}, {40,50,60}, {70,80,90}, {100,110,120}, {130,140,150} }
1์ฐจ์ ๋ฐฐ์ด
0x100 | ||||||||
score1 | 0x100 | <--- | score1 | 0 | 0 | 0 | 0 | 0 |
score1[0] | score2[1] | score3[2] | score4[3] | score5[4] |
0x100 | |||||||
score1 | 0x100<--- | score1 | 10 | 20 | 30 | 40 | 50 |
score1[0] | score2[1] | score3[2] | score4[3] | score5[4] |
0x100 | 0x200 | |||||
score2 | 0x100<--- | score2[0] | 0x200<--- | 0 | 0 | 0 |
score2[0][0] | score2[0][1] | score2[0][2] | ||||
score2[1] | 0x300<--- | 0x300 | ||||
0 | 0 | 0 | ||||
score2[1][0] | score2[1][1] | score2[1][2] | ||||
score2[2] | 0x400<--- | 0x400 | ||||
0 | 0 | 0 | ||||
score2[2][0] | score2[2][1] | score2[2][2] | ||||
score2[3] | 0x500<--- | 0x500 | ||||
0 | 0 | 0 | ||||
score2[3][0] | score2[3][1] | score2[3][2] | ||||
score2[4] | 0x600<--- | 0x600 | ||||
0 | 0 | 0 | ||||
score2[4][0] | score2[4][1] | score2[4][2] |
์์ ) ๋ฐฐ์ด ์์ ๋ฐฐ์ด
public class P192ArrayInArrayExample { public static void main(String[] args) { int[][] mathScores = new int[2][3]; for (int i=0; i<mathScores.length; i++) { for (int k=0; k<mathScores[i].length; k++) { System.out.println("mathScores["+i+"]["+k+"]="+mathScores[i][k]); } } System.out.println(); int[][] englishScores = new int[2][]; //์ด์ ๋ฃ๊ธฐ ๋๋ฆ์ด๋ค. //์ฒซ๋ฒ์งธ ์ค(index 0)์๋ 2์นธ englishScores[0] = new int[2]; //๋๋ฒ์งธ ์ค(index 1)์๋ 3์นธ englishScores[1] = new int[3]; for (int i=0; i<englishScores.length; i++) { for (int k=0; k<englishScores[i].length; k++) { System.out.println("englishScores["+i+"]["+k+"]="+englishScores[i][k]); } } System.out.println(); //๋ ๋ฐฉ์์ผ๋ก ๊ฐ๋ฅํ๋ค.// /*int[][] javaScores = { {95,80}, {92,96,80} }; javaScores[0][0]=95; javaScores[0][1]=80; javaScores[1][0]=92; javaScores[1][1]=96; javaScores[1][2]=80;*/ int[][] javaScores = {{95,80},{92,96,80}}; for(int i = 0; i<javaScores.length; i++) { for(int k = 0; k<javaScores[i].length; k++) { System.out.println("javaScores["+i+"]["+k+"]="+javaScores[i][k]); } } } }
๊ฒฐ๊ณผ)
mathScores[0][0]=0 mathScores[0][1]=0 mathScores[0][2]=0 mathScores[1][0]=0 mathScores[1][1]=0 mathScores[1][2]=0 englishScores[0][0]=0 englishScores[0][1]=0 englishScores[1][0]=0 englishScores[1][1]=0 englishScores[1][2]=0 javaScores[0][0]=95 javaScores[0][1]=80 javaScores[1][0]=92 javaScores[1][1]=96 javaScores[1][2]=80
7) ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๋ฐฐ์ด p. 193
์์ ) ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ ๋ฐฐ์ด
0x100 | |||||||
strArray | 0x100<--- | strArray | Java | Java | 0x200<--- | 0x200 | |
strArray[0] | strArray[1] | strArray[2] | String | Java |
public class P195ArrayReferenceObjectExample { public static void main(String[] args) { String[] strArray = new String[3]; strArray[0] = "Java"; strArray[1] = "Java"; strArray[2] = new String("Java");//new: ์๋ก์ด ์ฃผ์๋ฅผ ๋ง๋ ๋ค. System.out.println(strArray[0] == strArray[1]);//Java == Java: true System.out.println(strArray[0] == strArray[2]);//Java == 0x200: false System.out.println(strArray[0].equals(strArray[2]));//Java == Java: true } }
๊ฒฐ๊ณผ)
true false true
8) ๋ฐฐ์ด ๋ณต์ฌ p. 195
๋ฐฐ์ด ๊ฐ์ ํญ๋ชฉ ๊ฐ๋ค์ ๋ณต์ฌํ๋ ค๋ฉด for๋ฌธ์ ์ฌ์ฉํ๊ฑฐ๋ System.arraycopy() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
์์ ) for๋ฌธ์ผ๋ก ๋ฐฐ์ด ๋ณต์ฌ
0x100 | |||||||
oldIntArray | 0x100<--- | oldIntArray | 1 | 2 | 3 | ||
oldIntArray[0] | oldIntArray[1] | oldIntArray[2] | |||||
0x200 | |||||||
newIntArray | 0x200<--- | newIntArray | 0 | 0 | 0 | 0 | 0 |
newIntArray[0] | newIntArray[0] | newIntArray[0] | newIntArray[0] | newIntArray[0] |
public class P195ArrayCopyByForExample { public static void main(String[] args) { int[] oldIntArray = {1,2,3}; //1ํ์ 3์ด์ 1์ฐจ์๋ฐฐ์ด int[] newIntArray = new int[5]; /* ์๋ฆฌ * newIntArray[0]=oldIntArray[0]; * newIntArray[1]=oldIntArray[1]; * newIntArray[2]=oldIntArray[2]; */ //๋ณต์ฌํ๋ค. for(int i=0; i<oldIntArray.length; i++) { newIntArray[i] = oldIntArray[i]; } //๋ณต์ฌ๋ ๋ด์ฉ์ ์ถ๋ ฅํ๋ค. for(int i=0; i<newIntArray.length; i++) { System.out.print(newIntArray[i] + ", "); } } }
๊ฒฐ๊ณผ) ๋ณต์ฌ๋์ง ์์ ํญ๋ชฉ์ int[] ๋ฐฐ์ด์ ๊ธฐ๋ณธ ์ด๊ธฐ๊ฐ 0์ด ๊ทธ๋๋ก ์ ์ง๋๋ค.
1, 2, 3, 0, 0,
System.arraycopy()๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ด๋ค.
// ์๋ณธ ๋ฐฐ์ด, ์๋ณธ ๋ฐฐ์ด ์์ ์ธ๋ฑ์ค, ๋ณต์ฌ๋ณธ ๋ฐฐ์ด, ๋ณต์ฌ๋ณธ ๋ฐฐ์ด ์์ ์ธ๋ฑ์ค, ๊ธธ์ด System.arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
src ๋งค๊ฐ๊ฐ: ์๋ณธ ๋ฐฐ์ด
srcPos: ์๋ณธ ๋ฐฐ์ด์์ ๋ณต์ฌํ ํญ๋ชฉ์ ์์ ์ธ๋ฑ์ค
dest ๋งค๊ฐ๊ฐ: ์ ๋ฐฐ์ด
desPos: ์ ๋ฐฐ์ด์์ ๋ถ์ฌ๋ฃ์ ์์ ์ธ๋ฑ์ค
length: ๋ณต์ฌํ ๊ฐ์
์๋ณธ ๋ฐฐ์ด์ด arr1์ด๊ณ ์ ๋ฐฐ์ด์ด arr2์ผ ๊ฒฝ์ฐ arr1์ ๋ชจ๋ ํญ๋ชฉ์ arr2์ ๋ณต์ฌํ๋ ค๋ฉด ์๋์ ๊ฐ๋ค.
System.arraycopy(arr1, 0, arr2, 0, arr1.length);
์์ ) System.arraycopy()๋ก ๋ฐฐ์ด ๋ณต์ฌ
0x100 | |||||||
oldIntArray | 0x100<--- | oldIntArray | java | array | copy | ||
oldIntArray[0] | oldIntArray[1] | oldIntArray[2] | |||||
0x200 | |||||||
newIntArray | 0x200<--- | newIntArray | null | null | null | null | null |
newIntArray[0] | newIntArray[0] | newIntArray[0] | newIntArray[0] | newIntArray[0] |
public class P196ArrayCopyExample { public static void main(String[] args) { String[] oldStrArray = {"java", "array", "copy"}; String[] newStrArray = new String[5]; //arrayCopy ๋ฉ์๋๋ฅผ ํ์ฉํด์ ๋ณต์ฌํ๋ค. // ์๋ณธ ๋ฐฐ์ด, ์๋ณธ ๋ฐฐ์ด ์์ ์ธ๋ฑ์ค, ๋ณต์ฌ๋ณธ ๋ฐฐ์ด, ๋ณต์ฌ๋ณธ ๋ฐฐ์ด ์์ ์ธ๋ฑ์ค, ๊ธธ์ด System.arraycopy(oldStrArray, 0, newStrArray, 0, oldStrArray.length); //์ถ๋ ฅํ๋ค. for(int i=0; i<newStrArray.length; i++) { System.out.println(newStrArray[i] + ", "); } } }
๊ฒฐ๊ณผ)
java, array, copy, null, null,
9) ํฅ์๋ for๋ฌธ p. 197
์๋ฐ๋ ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์
์ ์ข ๋ ์ฝ๊ฒ ์ฒ๋ฆฌํ๊ธฐ ์ํด ํฅ์๋ for๋ฌธ์ ์ ๊ณตํ๋ค.
ํฅ์๋ for๋ฌธ์ ๋ฐ๋ณต ์คํ์ ํ๊ธฐ ์ํด ๋ฃจํ ์นด์ดํฐ ๋ณ์์ ์ฆ๊ฐ์์ ์ฌ์ฉํ์ง ์๋๋ค.
for๋ฌธ์ ๊ดํธ ()์๋ ๋ฐฐ์ด์์ ๊บผ๋ธ ํญ๋ชฉ์ ์ ์ฅํ ๋ณ์ ์ ์ธ๊ณผ ์ฝ๋ก (:) ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด์ ๋๋ํ ์์ฑํ๋ค.
๋ฐฐ์ด ๋ฐ ์ปฌ๋ ์
ํญ๋ชฉ์ ๊ฐ์๋งํผ ๋ฐ๋ณตํ๊ณ , ์๋์ ์ผ๋ก for๋ฌธ์ ๋น ์ ธ๋๊ฐ๋ค.
์๋์ ํํ๋ฅผ ๋งํ๋ค.
for (ํ์
๋ณ์ : ๋ฐฐ์ด){ ์คํ๋ฌธ; } for (int score : scores){ sum += score; }
๋ฐ๋ณตํ ๋๋ง๋ค ๋ณ์์๋ ๋ฐฐ์ด์์ ๊ฐ์ ธ์จ ํญ๋ชฉ์ด ์ ์ฅ๋๋ค.
์๋ฐ for([2]ํ์
๋ณ์ : [1]๋ฐฐ์ด){ //๋ฐฐ์ด์ ๊ธธ์ด ๋ฐ๋ณตํ๋ค. [3]์คํ๋ฌธ } ์๋ฐ์คํฌ๋ฆฝํธ for ๋ณ์ in ๋ฐฐ์ด{ }
1. for๋ฌธ์ด ์ฒ์ ์คํ๋ ๋ [1]๋ฐฐ์ด์์ ๊ฐ์ ธ์ฌ ์ฒซ ๋ฒ์งธ ๊ฐ์ด ์กด์ฌํ๋์ง ํ๊ฐํ๋ค.
2. ๊ฐ์ ธ์ฌ ๊ฐ์ด ์กด์ฌํ๋ฉด ํด๋น ๊ฐ์ [2]๋ณ์์ ์ ์ฅํ๋ค.
3. [3]์คํ๋ฌธ์ ์คํํ๋ค.
4. ๋ธ๋ก ๋ด๋ถ์ [3]์คํ๋ฌธ์ด ๋ชจ๋ ์คํ๋๋ฉด ๋ค์ ๋ฃจํ๋ฅผ ๋์ [1]๋ฐฐ์ด์์ ๊ฐ์ ธ์ฌ ๋ค์ ๊ฐ์ด ์กด์ฌํ๋์ง ํ๊ฐํ๋ค.
5. ๋ง์ฝ ๋ค์ ํญ๋ชฉ์ด ์กด์ฌํ๋ฉด [2]๋ณ์ โ [3]์คํ๋ฌธ โ [1]๋ฐฐ์ด ์์๋ก ๋ฃจํ๋ฅผ ๋ค์ ์งํํ๊ณ , ๊ฐ์ ธ์ฌ ๋ค์ ํญ๋ชฉ์ด ์์ผ๋ฉด for๋ฌธ์ด ์ข
๋ฃ๋๋ค.
๋ฐ๋ผ์ for๋ฌธ์ ๋ฐ๋ณต ํ์๋ ๋ฐฐ์ด์ ํญ๋ชฉ์๊ฐ ๋๋ค.
์ฐธ๊ณ ํด๋ณด๊ธฐ: https://engpro.tistory.com/196
์์ ) ํฅ์๋ for๋ฌธ
0x100 | |||||||||
scores | 0x100<--- | scores | 95 | 71 | 84 | 93 | 87 | sum | 430 |
scores[0] | scores[0] | scores[0] | scores[0] | scores[0] | |||||
์ฒซ๋ฒ์งธ for | score | 95 | |||||||
๋๋ฒ์งธ for |
score | 71 | |||||||
์ธ๋ฒ์งธ for |
score | 84 | |||||||
๋ค๋ฒ์งธ for |
score | 93 | |||||||
๋ค์ฏ๋ฒ์งธ for |
score | 87 |
for (int score : scores){
sum += score;
}
for (ํ์
๋ณ์ : ๋ฐฐ์ด){
์คํ๋ฌธ;
}
public class P198AdvancedForExample { public static void main(String[] args) { int[] scores = {95, 71, 84, 93, 87}; int sum = 0; //for(int i=0; i<scores.length; i++){ //์ผ๋ฐ์ ์ธ for for (int score : scores) { //ํฅ์๋ for๋ฌธ sum += score; //sum = sum + scores[i]; } System.out.println("์ ์ ์ดํฉ = " + sum); double avg = (double) sum / scores.length; System.out.println("์ ์ ํ๊ท = " + avg); } }
๊ฒฐ๊ณผ)
์ ์ ์ดํฉ = 430 ์ ์ ํ๊ท = 86.0
๋ง๋ฌด๋ฆฌ
7๊ฐ์ง ํค์๋๋ก ๋๋ด๋ ํต์ฌ ํฌ์ธํธ p. 199
1) ๋ฐฐ์ด: ๊ฐ์ ํ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ฐ์๋ ๊ณต๊ฐ์ ๋์ดํ๊ณ , ๊ฐ ๋ฐ์ดํฐ์ ์ธ๋ฑ์ค(index)๋ฅผ ๋ถ์ฌํด ๋์ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
2) ์ธ๋ฑ์ค: ๋ฐฐ์ด ํญ๋ชฉ์ ๋ถ์ธ ๋ฒํธ๋ฅผ ๋งํ๋ค. 0๋ฒ๋ถํฐ ์์ํ๋ฉฐ 0~(๋ฐฐ์ด๊ธธ์ด -1)๊น์ง ๋ฒ์๋ฅผ ๊ฐ์ง๋ค.
3) ๋ฐฐ์ด ์ ์ธ: ๋ณ์ ์ ์ธ๊ณผ ๋น์ทํ๋ฐ, ๋๊ดํธ๊ฐ ์ถ๊ฐ๋์ด ํ์
[] ๋ณ์; ํํ๋ก ์ ์ธ๋๋ค.
int[] scores์ int ๋ฐฐ์ด์ ์ ์ธํ๋ค. (scores: ๋ฐฐ์ด ๋ณ์)
4) ๋ฐฐ์ด ์์ฑ: ๋ฐฐ์ด์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ ๊ฐ์ง์ด๋ค.
1. {๊ฐ1, ๊ฐ2, ๊ฐ3, ... }์ฒ๋ผ ๊ฐ ๋ชฉ๋ก์ผ๋ก ์์ฑํ๋ค.
2. new ํ์
[๊ธธ์ด];๋ก ์์ฑํ๋ค.
5) ๋ฐฐ์ด ๊ธธ์ด: ๋ฐฐ์ด์ ์ ์ฅ๋ ์ ์๋ ํญ๋ชฉ(๊ฐ)์ ์๋ฅผ ๋งํ๋ค. ๋ฐฐ์ด์ length ํ๋์ ์ ์ฅ๋์ด ์๊ธฐ ๋๋ฌธ์ ๋ณ์.length๋ก ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ์ฝ์ ์ ์๋ค.
6) ๋ค์ฐจ์ ๋ฐฐ์ด: ๋ฐฐ์ด ํญ๋ชฉ(๊ฐ)์ด ๋ ๋ค๋ฅธ ๋ฐฐ์ด์ ์ฐธ์กฐํ ๋ ์ด๊ฒ์ ๋ค์ฐจ์ ๋ฐฐ์ด์ด๋ผ๊ณ ํ๋ค.
๋ค์ฐจ์ ๋ฐฐ์ด์ ์ ์ธ๊ณผ ์์ฑ ๋ฐฉ๋ฒ์ ํ์
[] [] ๋ณ์ = new ํ์
[๊ธธ์ด1][๊ธธ์ด2];์ด๋ค.
๋ณ์.length๋ 1์ฐจ์ ๋ฐฐ์ด์ ๊ธธ์ด1์ด ๋๊ณ ,
๋ณ์[์ธ๋ฑ์ค].length๋ ํด๋น ํญ๋ชฉ์ด ์ฐธ์กฐํ๋ 2์ฐจ์ ๋ฐฐ์ด์ ๊ธธ์ด2๋ฅผ ๋งํ๋ค.
7) ํฅ์๋ for๋ฌธ: for(ํ์
๋ณ์ : ๋ฐฐ์ด){...}์ ๋งํ๋ค.
ํฅ์๋ for๋ฌธ์ ๋ฐ๋ณต ์คํ์ ํ๊ธฐ ์ํด ๋ฃจํ ์นด์ดํฐ ๋ณ์์ ์ฆ๊ฐ์์ ์ฌ์ฉํ์ง ์๋๋ค.
๋ฐฐ์ด์ ํญ๋ชฉ ๊ฐ์๋งํผ ๋ฐ๋ณตํ๊ณ , ์๋์ ์ผ๋ก for๋ฌธ์ ๋น ์ ธ๋๊ฐ๋ค.
๋ฐ๋ณตํ ๋๋ง๋ค ๋ณ์์๋ ๋ฐฐ์ด์์ ๊ฐ์ ธ์จ ํญ๋ชฉ์ด ์ ์ฅ๋๋ค.
ํ์ธ ๋ฌธ์ p. 200
Q4) for๋ฌธ์ ์ด์ฉํด์ ์ฃผ์ด์ง ๋ฐฐ์ด์ ํญ๋ชฉ์์ ์ต๋๊ฐ์ ๊ตฌํ๋ค.
์์ ) for ๋ฌธ ์์ int i = 0; ์ผ๋ก ๋๋ค.
์ต๋๊ฐ ๊ตฌํ๋ ๊ฑฐ๋๊น max < array[i]๊ฐ ์กฐ๊ฑด์์ด๋ค.
max | 0 | |||||||
array | 0x100 | <--- | array | 1 | 5 | 3 | 8 | 2 |
array[0] | array[1] | array[2] | array[3] | array[4] |
public class P200Q4 { public static void main(String[] args) { int max = 0; //์ต๋๊ฐ์ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์ (์์ ๊ฐ์ ๋๋ค.) int[] array = {1,5,3,8,2}; //์์ฑ ์์น for (int i = 0; i < array.length; i++) { //max๋ณ์์ ์ ์ฅ๋์ด ์๋ ๊ฐ๋ณด๋ค ๋ฐฐ์ด์ ์ ์ฅ๋์ด ์๋ ๊ฐ์ด ํฌ๋ฉด if (max < array[i]) { //๋ฐฐ์ด์ ์๋ ๊ฐ์ max๋ณ์์ ์ ์ฅํ๋ค. max = array[i]; } } System.out.print("max: "+max); } }
๊ฒฐ๊ณผ)
max: 8
Q5) ์ค์ฒฉ for๋ฌธ์ ์ด์ฉํด์ ์ฃผ์ด์ง ๋ฐฐ์ด์ ์ ์ฒด ํญ๋ชฉ์ ํฉ, ํ๊ท ๊ฐ ๊ตฌํ๊ธฐ
์์ )
int count = 0; count++; ์ฐ๊ธฐ
array[i].length๋ก ํ๊ธฐํ๋ค.
0x100 | |||||||||
array | 0x100<--- | array[0] | 0x200<--- | 0x200 | 95 | 86 | |||
array[0][0] | array[0][1] | ||||||||
0x300<--- | 0x300 | 83 | 92 | 96 | |||||
array[1][0] | array[1][1] | array[1][2] | |||||||
0x400<--- | 0x400 | 78 | 83 | 93 | 87 | 88 | |||
array[2][0] | array[2][1] | array[2][2] | array[2][3] | array[2][4] |
public class P201Q5 { public static void main(String[] args) { int[][] array = { {95, 86}, {83, 92, 96}, {78, 83, 93, 87, 88} }; int sum = 0; double avg = 0.0; //์์ฑ ์์น //sum = ํฉ int count = 0; for (int i = 0; i<array.length; i++) { for (int j = 0; j<array[i].length; j++) { sum += array[i][j]; count++; } } avg = sum / count; System.out.println("sum: " + sum); System.out.println("average: " + avg); } }
๊ฒฐ๊ณผ)
sum: 881 average: 88.0
Q6) ์ ๋ ฅ ๋ฐ์ ์ต๊ณ ์ ์ ๋ฐ ํ๊ท ์ ์๋ฅผ ๊ตฌํ๋ค.
int[] scores = new int[3]; int[] scores = null; scores = new int[3];
run | true |
studentNum | 3 |
scores | 0x100 |
์์ )
public class P201Q6 { public static void main(String[] args) { boolean run = true; //๊ณ์ ์คํ ํ ๊ฒ์ธ์ง? ์ ํ ๊ฒ์ธ์ง? int studentNum = 0; //ํ์ ์(ํค๋ณด๋๋ก ์
๋ ฅ์ ํ ์ ์๊ฒ ์ฒ๋ฆฌ) int[] scores = null; //๊ฐ ํ์์ ์ ์(ํค๋ณด๋๋ก ์
๋ ฅ์ ํ ์ ์๊ฒ ์ฒ๋ฆฌ) Scanner scanner = new Scanner(System.in); while(run) { System.out.println("===================================="); System.out.println("1. ํ์์ | 2. ์ ์์
๋ ฅ | 3. ์ ์๋ฆฌ์คํธ | 4. ๋ถ์ | 5. ์ข
๋ฃ"); System.out.print("์ ํ>"); int selectNo = Integer.parseInt(scanner.nextLine()); //ํ์์ if(selectNo == 1) { //์์ฑ ์์น System.out.println("ํ์์> "); studentNum = Integer.parseInt(scanner.nextLine()); scores = new int[studentNum]; //์ ์ ์
๋ ฅ } else if(selectNo == 2) { //์์ฑ ์์น for (int i = 0; i < scores.length; i++) { System.out.println("scores["+i+"]>"); scores[i] = Integer.parseInt(scanner.nextLine()); //์ ์๋ฆฌ์คํธ }} else if(selectNo == 3) { //์์ฑ ์์น for (int i = 0; i < scores.length; i++) { System.out.println("scores["+i+"]:"+scores[i]); //๋ถ์ }} else if(selectNo == 4) { //์์ฑ ์์น //์ต๊ณ ์ ์๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์ int max = 0; //ํฉ๊ณ ์ ์๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์ int sum = 0; //ํ๊ท ์ ์๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋ณ์ double avg = 0.0; for (int i = 0; i < scores.length; i++) { //์ต๊ณ ์ ์ if (max < scores[i]) { max = scores[i]; //ํฉ๊ณ ์ ์ sum += scores[i]; } } //ํ๊ท ์ ์ avg = (double) sum / studentNum; System.out.println("max: " + max); System.out.println("average: " + avg); //์ข
๋ฃ } else if(selectNo == 5) { run = false; } } System.out.println("ํ๋ก๊ทธ๋จ ์ข
๋ฃ"); } }
๊ฒฐ๊ณผ)
==================================== 1. ํ์์ | 2. ์ ์์
๋ ฅ | 3. ์ ์๋ฆฌ์คํธ | 4. ๋ถ์ | 5. ์ข
๋ฃ ์ ํ>1 ํ์์> 2 ==================================== 1. ํ์์ | 2. ์ ์์
๋ ฅ | 3. ์ ์๋ฆฌ์คํธ | 4. ๋ถ์ | 5. ์ข
๋ฃ ์ ํ>2 scores[0]> 100 scores[1]> 101 ==================================== 1. ํ์์ | 2. ์ ์์
๋ ฅ | 3. ์ ์๋ฆฌ์คํธ | 4. ๋ถ์ | 5. ์ข
๋ฃ ์ ํ>3 scores[0]:100 scores[1]:101 ==================================== 1. ํ์์ | 2. ์ ์์
๋ ฅ | 3. ์ ์๋ฆฌ์คํธ | 4. ๋ถ์ | 5. ์ข
๋ฃ ์ ํ>4 max: 101 average: 100.5 ==================================== 1. ํ์์ | 2. ์ ์์
๋ ฅ | 3. ์ ์๋ฆฌ์คํธ | 4. ๋ถ์ | 5. ์ข
๋ฃ ์ ํ>5 ํ๋ก๊ทธ๋จ ์ข
๋ฃ