JAVA ARRAY

이상혁·2024년 1월 10일
0

Array

// 사용법

package exercise.chapter_20;

public class ArrayDeclaration {
    public static void main(String[] args) {
        int[] intArr = new int[10];
        int[] intArr2 = new int[]{1,2,3,4};
        int[] intArr3 = {1,2,3,4};
    }
}

// 접근하기

package exercise.chapter_20;

public class ArrayIndex {
    public static void main(String[] args) {
        int[] studentScores = {90, 87, 88, 75, 99, 65};
        int score1 = studentScores[0];
    }
}

// 반복문 사용

package exercise.chapter_20;

public class ArrayFor {
    public static void main(String[] args) {
        int[] studentScores = {90, 87, 88, 75, 99, 65};

        for (int i = 0; i < studentScores.length; i++) {
            System.out.printf("$d", studentScores[i]);
        }
    }
}

array의 복사

얇은 복사

같은 물건에 여러 이름이 붙는 것이다.
같은 메모리 주소를 바라보는 것이다.

package exercise.chapter_21;

public class ArrayCopy {
    public static void main(String[] args) {
        int a = 5;
        int b = a;

        int[] arr1 = {1,2,3,4};
        int[] arr2 = arr1;

    }
}

깊은 복사

실제 물건도 이름도 다른 게 만든다.
즉 새로운 메모리에 새로 값을 할당하는 것이다.

package exercise.chapter_21;

public class TwoDimensionArrayCopy {
    public static void main(String[] args) {

        int[][] arr = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12}
        };

        int[][] arr2 = arr.clone();
    }
}
profile
개발 공부 하기 위해 만든 블로그

0개의 댓글