[Java ๊ธฐ์ด] 4. ๋ฐฐ์ด
๐ก3์ฅ ํผ๋๋ฐฑ Tip
๐ String ๋ง์ ์ฐ์ฐ์ ์ง์ํ๋ ๊ฒ์ด ์ข๋ค.
String result = โโ;
for (int i = 0; i < 3; i++) {
result = result + (i + ","); // "0,"
// "0, 1,"
// "0, 1, 2,"
}
System.out.println(result);
String
์final
๋ก ๊ตฌํ๋์ด์์.
- ์ต์ข ๊ฒฐ๊ณผ๋ง result์ ๋์ ๋๋ค.
์ ์ฝ๋์ ๊ฒฐ๊ณผ๋ ์ ์์ ์ผ๋ก ์ถ๋ ฅ ๋์ง๋ง, ๋ํด์ง ๋ชจ๋ ๊ฒฝ์ฐ์ ๋ํด ์๋ก ๋ค๋ฅธ Stirng ๊ฐ์ฒด๊ฐ ๋ฉ๋ชจ๋ฆฌ ์์ ๊ฐ๊ฐ ์์ฑ๋์ด overhead๊ฐ ๋ฐ์ํ๋ค.
๋ฐ๋ผ์, ์ผ๋ฐ String ๊ฐ์ฒด์ ๊ฐ์ ๋ํ๋ ๋ฐฉ๋ฒ์ด ์๋, StringBuilder๋ฅผ ์ฌ์ฉํ์ฌ append()๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข๋ค.
nextLine() ์ฌ์ฉ ๋ฌธ์
int input = sc.nextInt();
sc.nextLine(); // ์
๋ ฅ๋ฒํผ๋ฅผ ๋น์์ฃผ๋ ์ญํ ์ ํ๋ค.
System.out.println("์ด๋ฆ>");
name = sc.nextLine();
nextLine() ํน์ง
- ํ ์ค ๋จ์๋ก ์ ๋ ฅํ๋ค.
- ์ฒ์ Enter ์ ๋ ฅ์ ์ํ์ง ์๋ ๊ฐํ ๋ฌธ์ ๊น์ง ์ฝ๋๋ค.
- ๋ฐ๋ผ์ ํ๋ฒ ๋ nextLine()์ ์์ ์ ์ฌ์ฉํจ์ผ๋ก์จ ๊ฐํ ๋ฌธ์๋ฅผ ์ ๊ฑฐํ ์ ์๋ค.
๐ 4์ฅ ๋ฐฐ์ด
๋ฐฐ์ด(array) ์ด๋ ๋์ผ ์ข ๋ฅ์ ๋ณต์ ๋ฐ์ดํฐ๋ฅผ ์์๋๋ก ์ ์ฅํ๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ
Java ๋ฐฐ์ด(Array) ์์ฑ
int [] scores; // ๋ฐฐ์ด๋ณ์์ ์ ์ธ
scores = new int[5]; // ์์์ ์์ฑ๊ณผ ๋์
int[] scores = new int[5];
๋ฐฐ์ด ๊ธธ์ด ๊ตฌํ๊ธฐ
int[] scores = new int[5];
int count = scores.length;
System.out.println("๋ฐฐ์ด์ ๊ธธ์ด: " + count);
๋ฐฐ์ด ์์ ๊ฐ ๋์
int[] scores = new int[5];
for (int i = 0; i<scores.length; i++){
scores[i] = i; // ๋ฐฐ์ด ๊ฐ ๋์
}
int [] scores1 = new int[] {10, 20, 30, 40, 50};
int [] scores2 = {1, 2, 3, 4, 5};
๋ฐฐ์ด์ ์ด๊ธฐํ


IndexOutOfBoundsException
- ๋ฐฐ์ด ๋ฒ์ ๋ฒ์ด๋ ์์ ์ด์ฉํ ๋ ์์ธ(exception) ๋ฐ์
package com.example;
public class Main {
public static void main(String[] args) {
int[] scores = {1, 2, 3, 4, 5};
// index 5 ๋ฒ์๋ฅผ ๋ฒ์ด๋จ
int sum = scores[0] + scores[1] + scores[2] + scores[3] + scores[5];
double average = sum / 5;
System.out.println(sum);
System.out.println(average);
}
}

โ console์ ํ์๋ ์์ธ๋ฅผ ๋ณด๊ณ ๋๋ฒ๊น ํ ์ ์๋ค.
Debug Tool ์ฌ์ฉ
๋๋ฒ๊น ๋ชจ๋์์, ์ค๋ฅ๊ฐ ๋ฐ์ํ ๊ฒ ๊ฐ์ ์ง์ ์ break ํฌ์ธํธ๋ฅผ ์ ์ ํ ์ฌ์ฉํ์ฌ ๋ฒ๊ทธ๋ฅผ ์ฐพ๋๋ค.
F6
ํ์ค์ฉ ์คํ
F8
์ ์ฒด ์คํ
for each ๋ฌธ ( ํ์ฅ for ๋ฌธ )

package com.example;
public class Main {
public static void main(String[] args) {
int[] scores = {1, 2, 3, 4, 5};
int sum = 0;
for (int score : scores) { // for - each
sum += score;
}
System.out.println(sum);
}
}
- ๋ฐฐ์ด ์์์ ๋ํด ๋ฐ๋ณต์ ์ฝ๊ฒ ํ ์ ์๋ค.
๋ฉ๋ชจ๋ฆฌ์ ๋ณ์
// ๋ฐฐ์ด ๋ณ์ a๋ ์ฃผ์(reference)๊ฐ ์ ์ฅ๋จ
int[] a = { 1, 2, 3 };
int[] b;
b = a; // a ๋ฐฐ์ด์ ์์ ์ฃผ์๊ฐ b์ ์ ์ฅ๋จ
b[0] = 100;
System.out.println(a[0]);
// b[0]์ 100์ ๋ฃ์์ผ๋, a[0]๋ 100์ด ์ถ๋ ฅ๋จ.
์ฐธ์กฐ (reference)

๊ทธ ์ธ int, double, boolean ๋ฑ์ ๊ธฐ๋ณธํ(primitive type)๋ณ์์ ๊ตฌ๋ณ ๋๋ค.
๊ฐ๋น์ง ์ปฌ๋ ์ (garbage collection)
์ผ๋ฐ์ ์ผ๋ก ํจ์ ํธ์ถ๊ณผ ๊ด๋ จ๋ ์ง์ญ, ๋งค๊ฐ๋ณ์๋ ๋ฉ๋ชจ๋ฆฌ์ stack ์์ญ์ ์ ์ฅ๋๋ค. ๋ํ stack ์์ญ์ ํจ์ ํธ์ถ๊ณผ ํจ๊ป ํ ๋น๋๊ณ , ํจ์ ํธ์ถ์ด ์ข ๋ฃ๋๋ฉด pop๋์ด ์๋ฉธํ๋ค.
ํ์ง๋ง, new
๋ก ์ฌ์ฉ์์ ์ํด ๋์ ์ผ๋ก ์์ฑ๋ ๊ฐ์ฒด ์์๋ค์ ๋ฉ๋ชจ๋ฆฌ์ heap ์์ญ์ ์ ์ฅ๋๋ค.
new
๋ก ์์ฑ๋ ๊ฐ์ฒด ์์๋ค์ ํด๋น ๋ธ๋ก์ด ๋๋๋ ์๋ช
์ด ๋คํ์ง ์๊ณ , ๋ฉ๋ชจ๋ฆฌ ์์ ์กด์ฌํ๋ค.ChatGPT ์ค๋ช
Java์ GC๋ JVM (Java Virtual Machine)์ ์ํด ์ํ๋ฉ๋๋ค. JVM์ ์คํ ์ค์ธ Java ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ ์ง์์ ์ผ๋ก ๋ชจ๋ํฐ๋งํ๋ฉฐ, ์ฌ์ฉ๋์ง ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์๋ณํ์ฌ ์๋์ผ๋ก ํด์ ํฉ๋๋ค. ์ด๋ฌํ ๊ณผ์ ์์, Java ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฑ๋ฅ์ด ํฅ์๋๋ฉฐ, ๊ฐ๋ฐ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์ ๋ ์ด์ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋ฉ๋๋ค.
๐ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ


package com.example;
public class Main {
public static void main(String[] args) {
boolean b = true;
if(b == true) {
int[] nums = new int[] { 1, 2, 3 };
}
System.out.println(nums[1]); // ์ปดํ์ผ ์๋ฌ
}
}
- โnumsโ ๋ณ์๋ ๋ธ๋ก ๋ด๋ถ ์ ์ธ๋ ๋ณ์์ด๋ฏ๋ก ์ฌ์ฉ ๋ถ๊ฐํ๋ค.
- ํ์ง๋ง new๋ก์ ์์ฑ๋ { 1, 2, 3 } ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ๋ด์ ์กด์ฌํ๋ค.
- GC์ ์ํด ๋์ ๋ฉ๋ชจ๋ฆฌ ํด์ ๊ฐ ์๋์ผ๋ก ๋๋ค.
null

null์ ์ฐธ์กฐ ์ ๊ฑฐ ๊ธฐ๋ฅ
๐ฏ ์ฐธ์กฐํ ๋ณ์์ ์ดํด
public class Main {
public static void main(String[] args) {
int[] a = { 1, 2, 3 };
int[] b;
b = a;
a = null;
System.out.println(b[0]);
}
}
- ๋ฐฐ์ด b์ ๋ฐฐ์ด a ์ฐธ์กฐํ ๋ณ์๊ฐ ๊ฐ๋ฆฌํค๋ ์ฃผ์๋ฅผ ๋ฃ๋๋ค.
- ์ดํ a๋ฅผ null๋ก ์ด๊ธฐํ ํ๊ณ b๋ฅผ ์ถ๋ ฅํ๋ฉด, ์๋ a๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
์ ์ฝ๋์ ๋ํ ๊ทธ๋ฆผ ์ค๋ช



๋ค์ฐจ์ ๋ฐฐ์ด

- ๋ค์ฐจ์ ๋ฐฐ์ด ๊ตฌ์กฐ
package com.example;
public class Main {
public static void main(String[] args) {
int[][] scores = {{10, 20, 30}, {30, 40, 50}};
System.out.println(scores.length);
System.out.println(scores[0].length);
}
}


๐์ฐ์ต ๋ฌธ์
4-1
๋ค์ ์กฐ๊ฑด์ ๋ง๋ ๊ฐ ๋ฐฐ์ด์ ์ค๋นํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค. ๊ฐ์ ์ด๊ธฐํ๋ ํ์ ์์.
- int ํ ๊ฐ์ 4๊ฐ ๋ด์ ์ ์๋ ๋ฐฐ์ด points
- double ํ ๊ฐ์ 5๊ฐ ๋ด์ ์ ์๋ ๋ฐฐ์ด weights
- boolean ํ ๊ฐ์ 3๊ฐ ๋ด์ ์ ์๋ ๋ฐฐ์ด answers
- String ํ ๊ฐ์ 3๊ฐ ๋ด์ ์ ์๋ ๋ฐฐ์ด names
package com.example;
public class Exam4_1 {
public static void main(String[] args) {
int[] points = new int[4];
double[] weights = new double[5];
boolean[] answers = new boolean[3];
String[] names = new String[3];
}
}
4-2
๋ค์ ์กฐ๊ฑด์ ๋ง๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
- 3๊ฐ์ ๊ณ์ข ์์ก โ121902โ, โ8302โ, โ55100โ ์ด ๋ด๊ฒจ ์๋ int ํ ๋ฐฐ์ด moneyList ๋ฅผ ์ ์ธํ์์ค
- ๊ทธ ๋ฐฐ์ด์ ์์๋ฅผ 1๊ฐ์ฉ for ๋ฌธ์ผ๋ก ๊บผ๋ด์ ํ๋ฉด์ ํ์ํ์์ค
- ๊ฐ์ ๋ฐฐ์ด ์์๋ฅผ foreach ๋ฌธ์ผ๋ก 1๊ฐ์ฉ ๊บผ๋ด์ ํ๋ฉด์ ํ์ํ์์ค
package com.example;
public class Exam4_2 {
public static void main(String[] args) {
int[] moneyList = {121902, 8302, 55100};
// ์ผ๋ฐ for๋ฌธ์ ํตํ ๋ฐฐ์ด ์์ ๊ฐ์ ธ์ค๊ธฐ
for (int i = 0; i < moneyList.length; i++) {
System.out.println(moneyList[i]);
}
// for - each ๋ฌธ์ ํตํ ๋ฐฐ์ด ์์ ๊ฐ์ ธ์ค๊ธฐ
for (int money : moneyList) {
System.out.println(money);
}
}
}
4-3

๋ต


- ์์ธ 1: java.lang.NullPointerException
- ์์ธ 2: java.lang.ArrayIndexOutOfBoundsException
4-4
๋ค์ 4๊ฐ์ ์กฐ๊ฑด์ ๋ง๋ โ์ซ์ ๋ง์ถ๊ธฐ ํด์ฆ" ํ๋ก๊ทธ๋จ์ ์์ฑ ํ์์ค.
- 3๊ฐ์ง๋ฆฌ intํ ๋ฐฐ์ด numbers๋ฅผ ์ค๋นํ์์ค. ์ด ๋ ์ด๊ธฐํ๋ ๊ฐ๊ฐ 3, 4, 9 ๋ก ํฉ๋๋ค.
- ํ๋ฉด์ โ1์๋ฆฌ์ ์ซ์๋ฅผ ์ ๋ ฅ ํด ์ฃผ์ธ์" ๋ผ๊ณ ํ์ํฉ๋๋ค
- ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํด ํค๋ณด๋๋ก๋ถํฐ ์ซ์๋ฅผ ์ ๋ ฅ ๋ฐ์, ๋ณ์ input ์ ๋์ ํฉ๋๋ค input๊ฐ์ด 3, 4, 9 ์ค ํ๋์ ๊ฐ๋ค๋ฉด โ์ ๋ต!โ ์ด๋ผ๊ณ ํ์ํฉ๋๋ค.

- input๊ฐ์ด 3, 4, 9 ์ค ํ๋์ ๊ฐ๋ค๋ฉด โ์ ๋ต!โ ์ด๋ผ๊ณ ํ์ํฉ๋๋ค.
package com.example;
public class Exam4_4 {
public static void main(String[] args) {
int[] numbers = {3, 4, 9};
System.out.println("1์๋ฆฌ์ ์ซ์๋ฅผ ์
๋ ฅ ํด ์ฃผ์ธ์");
int input = new java.util.Scanner(System.in).nextInt();
for (int number : numbers) {
if (number == input) {
System.out.println("์ ๋ต!");
break;
}
}
}
}


ํด์ฆ
โญ ๋ฌธ์์ด ๊ฒฐํฉ - ๋ฐฐ์ด๋ก ํ๊ธฐ
์ ๋ ฅ ๋ฐ์ ๋ฌธ์์ด ์์ ๋ฌธ์์ด์ ์ ๋ ฅ๋ฐ๊ณ ์ถ๋ ฅ ํฌ๋ฉง์ ๋ง๊ฒ ์ถ๋ ฅํ์ธ์.


import java.util.Scanner;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] names = new String[n + 1];
names[0] = "Hello ";
for (int i = 1; i < n + 1; i++) {
String input = sc.next();
if (i == n) {
names[i] = input + ".";
} else {
names[i] = input + ",";
}
}
for (String name : names) {
System.out.print(name);
}
}
}

Uploaded by N2T