public class Ex01_array {
public static void main(String[] args) {
int []a = new int[] {10,20,30};
for(int i=0; i<a.length; i++){
System.out.print(a[i]+" ");
}
System.out.println();
int []b = new int[5];
System.arraycopy(a, 0, b, 0, a.length);
// arraycopy(원본배열, 본사시킬 원본 배열 시작위치, 대상배열, 복사시킬 위치, 복사할개수)
for(int i=0; i<b.length; i++){
System.out.print(b[i]+" ");
}
System.out.println();
a = b;
// b 배열의 주소를 a에게 할당.
// 원래 a의 주소는 사라지게 된다.
for(int i=0; i<a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println();
}
}
배열은 참조형 데이터타입이다.
배열은 초기화하지 않으면 0으로 초기화된다.
b의 주소값을 a에게 옮겼기 때문에 원래 a가 가지고 있던 3개의 값을 저장한 주소는 찾을 수 없게 되므로 garbage collector의 회수 대상이 되어 사라진다.
'쌍용강북교육센터 > 7월' 카테고리의 다른 글
0713_Ex03_array : 2차원 배열 복사 (0) | 2021.07.13 |
---|---|
0713_Ex02_array : 배열 비교해보기 (0) | 2021.07.13 |
0712_Ex05_bubble : 버블정렬에 대해 (1) | 2021.07.13 |
0712_Ex04_selection : 선택정렬에 대해 (0) | 2021.07.13 |
0712_Ex03_Quiz : 로또 1~45까지 수중 서로 다른 6개의 수 생성 (0) | 2021.07.12 |