728x90
배열
참조형 data type: String
JVM 메모리 영역
스택 - 힙
참조형: String, 배열 → heap에 주소 값을 갖고 있음, 주소 1을 stack에 저장하여 힙의 값을 참조함
new
로 생성: stack에 생성됨
String str1 = "서울"; //literal
String str2 = new String("서울"); //object 객체
String str3 = str2;
==
로 비교할 때, true
나올 수도 있음
배열의 생성과 초기화
자료형[] 배열이름 = {값1, 값2, 값3, 값4}
선언과 동시에 초기화
배열이름 = new 자료형[] {값1, 값2, 값3, 값4}
배열 생성 및 값 초기화
배열이름 = new 자료형[길이];
배열 생성 (자료형의 초기값으로 초기화)
타입 Date → dateArr, Date[] dateArr;
기본형 = null
기본자료형 제외하고 모두 null (참조형 변수)
int[] tmp = new int[5];
arr = tmp
→ 복사
int[] points = new int[3];
points[1] = 'A'; //ASCII 65
배열 선언: int[] points
배열 생성: new int[3];
참조 값 할당: points = new int[3];
2차원 배열
- 1차원 배열들의 배열: 모두 크기(길이)가 같을 필요 없음!
new 배열유형[1차원 배열개수][1차원 배열의 크기]
new 배열유형[1차원 배열개수][];
1차원 배열은 null
int a = 10;
int[] arr = new int [4];
int [][] arr2 = new int[2][];
arr2[0] = new int[3];
arr2[1] = new int[3];
arr2[1][1] = 100;
2차원 배열 탐색
0 | 1 | 2 | 3 | 4 | |
---|---|---|---|---|---|
0 | |||||
1 | r-1, c | ||||
2 | r, c-1 | r, c | r, c+1 | ||
3 | r+1, c | ||||
4 | |||||
- 사방 탐색 |
int[] dr = { -1, 1, 0, 0 };
int[] dc = { 0, 0, -1, 1 };
for (int r = 0; r < 5; r++) {
for (int c = 0; c < 5; c++) {
int sum = arr[r][c];
for (int d = 0; d < 4; d++) {
int nr = r + dr[d];
int nc = c + dr[d];
// 경계처리
if (nr >= 0 && nr < 5 && nc >=0 && nc <5) {
sum += arr[nr][nc];
}
}
}
}
- 2차원 배열 출력
System.out.println(Arrays.deepToString(arr));
ToString
: 배열의 참조값
728x90
'Java' 카테고리의 다른 글
[자바/Java] 제네릭 Generics (0) | 2022.07.27 |
---|---|
[자바/Java] 클래스 다형성(Polymorphism) (0) | 2022.07.25 |
[자바/Java] 클래스(Class), 상속, 생성자(Constructor), 오버라이딩(Override) (0) | 2022.07.25 |
[자바/Java] 싱글톤(Singleton) 패턴, Static (0) | 2022.07.25 |
자바 정규식 (regex) / 정규식 테스트 (0) | 2022.04.29 |