구조 설계 입문 — 배열 · static · 생성자

최병현·2025년 12월 8일

java

목록 보기
5/38
post-thumbnail

1. 오늘 배운 핵심 개념

1) 배열의 복사 — Shallow / Deep Copy

배열은 값이 아니라 주소를 저장하기 때문에 얕은 복사와 깊은 복사의 차이를 정확히 이해함.

✔ 얕은 복사

int[] a = {1, 2, 3};
int[] b = a; // 같은 주소 공유
b[0] = 99;
System.out.println(a[0]); // 99

✔ 깊은 복사

int[] a = {1, 2, 3};
int[] b = Arrays.copyOf(a, a.length);
b[0] = 99;
System.out.println(a[0]); // 1

2) Arrays 클래스

int[] arr = {3, 1, 5, 2};
System.out.println(Arrays.toString(arr)); // [3,1,5,2]
Arrays.sort(arr);
System.out.println(Arrays.toString(arr)); // [1,2,3,5]
int[] b = Arrays.copyOf(arr, arr.length);
System.out.println(Arrays.equals(arr, b)); // true

3) 2차원 배열 구조

int[][] score = {
    {90, 80, 70},
    {88, 75, 92},
    {100, 95, 90}
};
System.out.println(score[0][1]); // 80

✔ 전체 출력

for (int i = 0; i < score.length; i++) {
    for (int j = 0; j < score[i].length; j++) {
        System.out.print(score[i][j] + " ");
    }
    System.out.println();
}

4) 마방진 기본 구현

int n = 3;
int[][] magic = new int[n][n];
int row = 0;
int col = n / 2;
for (int num = 1; num <= n * n; num++) {
    magic[row][col] = num;
    int nextRow = (row - 1 + n) % n;
    int nextCol = (col + 1) % n;
    if (magic[nextRow][nextCol] != 0) {
        row = (row + 1) % n;
    } else {
        row = nextRow;
        col = nextCol;
    }
}

5) 클래스 개념

class Student {
    String name;
    int age;
    void study() {
        System.out.println(name + " 공부중");
    }
}

6) static (정적 변수)

class Counter {
    static int count = 0;
    Counter() {
        count++;
    }
}
new Counter();
new Counter();
new Counter();
System.out.println(Counter.count); // 3

7) Getter / Setter

class Student {
    private int age;
    public int getAge() { return age; }
    public void setAge(int age) { this.age = age; }
}

8) 생성자 / 오버로딩 / this

class Student {
    String name;
    int age;
    Student() {}
    Student(String name) {
        this.name = name;
    }
    Student(String name, int age) {
        this(name);
        this.age = age;
    }
}

2. 오늘 실습 1 — 2차원 배열 점수 프로그램

int[][] std = new int[3][3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < std.length; i++) {
    System.out.print("국어: ");
    std[i][0] = sc.nextInt();
    System.out.print("영어: ");
    std[i][1] = sc.nextInt();
    System.out.print("수학: ");
    std[i][2] = sc.nextInt();
}
for (int i = 0; i < std.length; i++) {
    System.out.println(Arrays.toString(std[i]));
}

3. 오늘 실습 2 — 클래스 + 생성자 연습

class Student {
    String name;
    int age;
    Student() {}
    Student(String name) {
        this.name = name;
    }
    Student(String name, int age) {
        this(name);
        this.age = age;
    }
    void print() {
        System.out.println(name + " / " + age);
    }
}
public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student("철수");
        Student s3 = new Student("영희", 22);
        s3.print();
    }
}

4. 어려웠던 점

  • 얕은/깊은 복사의 주소 개념이 처음엔 혼란스러웠음
  • 2차원 배열에서 i, j의 역할 구분이 어려웠음
  • static과 this의 차이가 처음엔 애매했음
  • 생성자 오버로딩 규칙 적응이 필요했음

5. 해결 & 깨달음

  • 배열은 값을 저장하는 게 아니라 ‘주소’를 저장함
  • 2차원 배열은 표(table)로 생각하면 쉽게 구조 파악 가능
  • static은 “공유 저장소”, instance는 “개별 저장소”
  • 생성자 오버로딩은 “초기화 방법 다양성” 제공

6. 내일 목표

  • 2차원 배열 문제 더 풀기
  • 클래스 기반 미니 프로젝트 진행
  • static/instance 변수 구분 연습
  • 생성자 오버로딩 + this() 완전 숙련

7. 느낀 점

오늘은 배열 → 구조화 → 객체 설계까지 연결되는 흐름을 제대로 잡은 날이었다. 2차원 배열과 클래스 개념이 연결되면서 프로그램이 단순 명령이 아닌 ‘구조적 사고’로 보이기 시작했다.

profile
Develop

0개의 댓글