끄적모음

요약 정리 - 자바 5

taehyon 2025. 4. 10. 20:40

📗 배울 내용

  1. 배열시작
  2. 배열의 선언과 생성
  3. 배열 사용
  4. 배열 리펙토링
  5. 2차원 배열 - 시작
  6. 2차원 배열 - 리팩토링1
  7. 2차원 배열 - 리팩토링2
  8. 향상된 for문
  9. 999

1. 배열시작

배열이 필요한 이유

 

public class Array1 {
    public static void main(String[] args) {
        int student1 = 90;
        int student2 = 80;
        int student3 = 70;
        int student4 = 50;
        int student5 = 40;
        int student6 = 40;

        System.out.println("학생1 점수 : "+ student1);
        System.out.println("학생2 점수 : "+ student2);
        System.out.println("학생3 점수 : "+ student3);
        System.out.println("학생4 점수 : "+ student4);
        System.out.println("학생5 점수 : "+ student5);
        System.out.println("학생6 점수 : "+ student6);
    }
}

같은 타입의 변수를 반복해서 선언하고 반복해서 사용하는 문제를 해결하는것이 바로 배열이다.

 

 


2. 배열의 선언과 생성

public class Array1Ref1 {
    public static void main(String[] args) {
        int[] students; // 배열형 변수 선언
        students = new int[5];

 

배열을 생성하고 나면 자바는 메모리 어딘가에 있는 이 배열에 접근 할 수 있는 참조값(주소)를 반환한다.

 

배열은 변수와 사용법이 비슷한데 차이점이 있다면 [ ] 사이에 숫자(번호)를 넣어주어야 한다는 것이다.

 

배열은 0 부터 시작하고 배열의 위치를 나타내는 숫자를 인덱스라고 한다.

 

선언한 배열의 크기보다 큰 배열을 만들었을때는 오류가 난다.


3. 배열 사용

public class Array1Ref1 {
    public static void main(String[] args) {
        int[] students; // 배열형 변수 선언
        students = new int[5];

        //변수 값 대입
        students[0] = 90; // 배열은 0부터 시작
        students[1] = 80;
        students[2] = 70;
        students[3] = 60;
        students[4] = 50;


        System.out.println("학생1 점수 : "+ students[0]);
        System.out.println("학생2 점수 : "+ students[1]);
        System.out.println("학생3 점수 : "+ students[2]);
        System.out.println("학생4 점수 : "+ students[3]);
        System.out.println("학생5 점수 : "+ students[4]);
    }
}

4. 배열 리펙토링

리펙토링은 기존의 코드 기능은 유지하면서 내부 구조를  개선하여 가독성을 높이고 유지 보수를 용이하게  하는 과정이다

 

public class Array1Ref4 {
    public static void main(String[] args) {
         // 배열형 변수 선언
        int[] students = {90,80,70,60,50}; //배열 생성과 초기화 동시에! 단 한줄로 있어야함
        // 리펙토링
        for(int i=0; i<students.length; i++){ // .length 는 현재 배열의 길이를 반환한다.
            System.out.println("학생"+ ( i + 1 ) +" 점수 : " + students[i]);
        }
    }
}
위의 코드와 기능은 같지만 내부 구조를 개선하여 가독성을 높인것이다

5.  2차원 배열 - 시작

 

2차원 배열


int [행][열]  

 

행은 row (가로) 열은 column(세로)

public class ArrayDi0 {

    public static void main(String[] args) {
        // 2x3 2차원 배열을 만든다.
        int[][] arr = new int[2][3];

        arr[0][0]=1; //0행 0열
        arr[0][1]=2; //0행 1열
        arr[0][2]=3; //0행 2열
        arr[1][0]=4; //1행 0열
        arr[1][1]=5; //1행 1열
        arr[1][2]=6; //1행 2열

        // 0행
        System.out.print(arr[0][0] + " ");
        System.out.print(arr[0][1] + " ");
        System.out.print(arr[0][2] + " ");
        System.out.println();//line change
        // 1행
        System.out.print(arr[1][0] + " ");
        System.out.print(arr[1][1] + " ");
        System.out.print(arr[1][2] + " ");
    }
}

6. 2차원 배열 - 리팩토링 1

public class ArrayDi2 {

    public static void main(String[] args) {
        // 2x3 2차원 배열을 만든다.
        int[][] arr = new int[2][3];

        arr[0][0]=1; //0행 0열
        arr[0][1]=2; //0행 1열
        arr[0][2]=3; //0행 2열
        arr[1][0]=4; //1행 0열
        arr[1][1]=5; //1행 1열
        arr[1][2]=6; //1행 2열

        for(int row=0; row<2; row++){
            for(int column=0; column<3; column++){
                System.out.print(arr[row][column] + " ");
            }
            System.out.println();
        }
    }
}

7. 2차원 배열 - 리팩토링 2

public class ArrayDi4 {

    public static void main(String[] args) {
        // 2x3 2차원 배열을 만든다.
        int[][] arr = new int[3][4];
        int i=1;
        // 2차원 배열 값을 넣고
        for(int row=0; row<arr.length;row++){
            for (int column = 0; column < arr[row].length; column++) {
                arr[row][column]=i++;
            }
        }
        // 출력
        for(int row=0; row<arr.length; row++){
            for(int column=0; column<arr[row].length; column++){
                System.out.print(arr[row][column] + " ");
            }
            System.out.println();
        }
    }
}

8. 향상된 for문

// 향상된 for문 for-each문
for (int number : numbers) {
	System.out.println(number);
}

// for-each 문을 사용할 수 없는 경우, 증가하는 index 값 필요
for (int i=0; i<numbers.length;i++){
	System.out.println("number" + i + "번의 결과는 : "+numbers[i]); 
    // 이렇게 i의 값이 필요할때는 사용을 못함
}

 

 


9.  배열 넘나 어렵당~

 

'끄적모음' 카테고리의 다른 글

클래스와 객체  (0) 2025.04.15
자바 공부 5  (1) 2025.04.11
요약 정리 - 자바 4  (0) 2025.04.10
요약 정리 - 자바  (0) 2025.04.09
요약정리 - 자바 3  (0) 2025.04.09