Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
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
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[28] 230207 Java Ch. 5 ์ฐธ์กฐ ํƒ€์ž…: 2. ๋ฐฐ์—ด [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 28์ผ] ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/Spring Framework, Java

[28] 230207 Java Ch. 5 ์ฐธ์กฐ ํƒ€์ž…: 2. ๋ฐฐ์—ด [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 28์ผ]

yjyuwisely 2023. 2. 7. 14:44

230207 Tue 28th class

Ch. 5 ์ฐธ์กฐ ํƒ€์ž…
์ง„๋„: p. 186 ~ (๊ต์žฌ: ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”, ์ €์ž: ์‹ ์šฉ๊ถŒ)

 

ํ˜ผ์ž ๊ณต๋ถ€ํ•˜๋Š” ์ž๋ฐ”

๋…ํ•™์œผ๋กœ ์ž๋ฐ”๋ฅผ ๋ฐฐ์šฐ๋Š” ์ž…๋ฌธ์ž๊ฐ€ ‘๊ผญ ํ•„์š”ํ•œ ๋‚ด์šฉ์„ ์ œ๋Œ€๋กœ’ ํ•™์Šตํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌ์„ฑํ–ˆ๋‹ค. ‘๋ฌด์—‡์„’ ‘์–ด๋–ป๊ฒŒ’ ํ•™์Šตํ•ด์•ผ ํ• ์ง€ ์กฐ์ฐจ ๋ชจ๋ฅด๋Š” ์ž…๋ฌธ์ž์˜ ๋ง‰์—ฐํ•œ ๋งˆ์Œ์„ ์‚ดํŽด, ๊ณผ์™ธ ์„ ์ƒ๋‹˜์ด

www.aladin.co.kr

 

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค. 


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ

System.out.println(); ์—”ํ„ฐ ํšจ๊ณผ๊ฐ€ ์žˆ๋‹ค. 

Continue: (//์Šคํ‚ต) break๋ฌธ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ continue๋ฌธ๋„ ๋Œ€๊ฐœ if๋ฌธ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ, 
ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ์— continue๋ฌธ์„ ์‹คํ–‰ํ•ด์„œ ๊ทธ ์ดํ›„์˜ ๋ฌธ์žฅ์„ ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ๋‹ค์Œ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

2023.02.03 - [1. Revision 2023/Java] - [26] 230204 Ch. 4 ์ž๋ฐ” ์กฐ๊ฑด๋ฌธ๊ณผ ๋ฐ˜๋ณต๋ฌธ(2): ๋ฐ˜๋ณต๋ฌธ: for๋ฌธ, while๋ฌธ, do-while๋ฌธ

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 null "10" -> 10 "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
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ

728x90
๋ฐ˜์‘ํ˜•
Comments