public class Array1 {
public static void main(String[] args) {
int[] students ; // 배열 변수 선언
students = new int[5]; // 배열 생성
// 변수 값 대입
students[0] = 90;
students[1] = 80;
students[2] = 70;
students[3] = 60;
students[4] = 50;
// 배열의 길이
int length = students.length;
// 변수값 사용 : 리팩터링
for (int i = 0; i < length; i++) {
System.out.println("학생" + (i + 1) + " 점수 : " + students[i]);
}
}
}
1-1. 배열 변수 선언
int[] students ; // 배열 변수 선언
· 배열을 사용하려면 int[] students; 와 같이 배열 변수를 선언해야 한다.
· 일반적인 변수와 차이점은 int[] 처럼 타입 다음에 대괄호([ ])가 들어간다.
1-2. 배열 생성
students = new int[5]; // 배열 생성
· new int[5] 라고 입력하면 그림과 같이 5개의 int형 변수가 만들어진다.
1-3. 배열과 초기화
· 자바는 배열을 생성할 때 그 내부값을 자동으로 초기화한다.
· 숫자는 0, boolean은 false, String은 null로 초기화 된다.
1-4. 배열 참조값(주소값) 보관
int[] students = new int[5]; //1. 배열 생성
int[] students = x001; //2. new int[5]의 결과로 x001 참조값 반환
students = x001 //3. 최종 결과
· new int[5]로 배열을 생성하면 배열의 크기만큼 메모리를 확보한다. (4byte × 5 → 20byte)
· int[] students 변수는 new int[5] 로 생성한 배열의 참조값(x001)을 가지고 있다.
· 참조값을 통해 메모리에 있는 실제 배열에 접근하고 사용할 수 있다.
2. 배열 리팩터링
2-1. 간단한 배열 초기화 (1)
배열은 중괄호({ })를 사용해서 생성과 동시에 초기화 가능을 제공한다.
package array;
public class Array2 {
public static void main(String[] args) {
// 리팩터링 : 배열 생성과 초기화
int[] students = new int[]{90, 80, 70, 60, 50};
// 배열의 길이
int length = students.length;
// 변수값 사용
for (int i = 0; i < length; i++) {
System.out.println("학생" + (i + 1) + " 점수 : " + students[i]);
}
}
}
2-2. 간단한 배열 초기화 (2)
배열은 중괄호({ })만 사용해서 new int[]를 생략하고 생성과 동시에 편리하게 초기화 하는 기능을 제공한다.
단 이때는 예제와 같이 배열 변수의 선언을 한 줄에 함께 사용할 때만 가능하다.
// 리팩터링 : 배열 생성과 초기화 - 생략 가능
int [] students = {90, 80, 70, 60, 50};
· 오류
int [] students;
students = {90, 80, 70, 60, 50};
3. 2차원 배열
3-1. 배열 변수 선언과 생성
// 2x3 2차원 배열
int[][] arr = new int[2][3]; // 행 : 2, 열 : 3
· 2차원 배열을 사용하려면int[][] arr;와 같이 배열 변수를 선언해야 한다.
· new int[2][3]; 라고 입력하면 그림과 같이 2행 3열의 2차원 배열이 만들어진다.
3-2. 행(row)과 열(Column)
int row = arr.length;
int column = arr[row].length;
3-3. 2차원 배열 초기화
// 2x3 2차원 배열 리팩터링 - new 생략 가능
int[][] arr = new int[][] {
{1, 2, 3}, // 0행
{4, 5, 6} // 1행
};
3-4. 실습 코드
package array;
public class Array3 {
public static void main(String[] args) {
// 2x3 2차원 배열
int[][] arr = new int[2][3]; // 행 : 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();
}
}
}
package array;
public class Array4 {
public static void main(String[] args) {
// 2x3 2차원 배열 리팩터링 - new 생략 가능
int[][] arr = new int[][]{
{1, 2, 3}, // 0행
{4, 5, 6} // 1행
};
// 리팩터링
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();
}
}
}
package array;
public class Array5 {
public static void main(String[] args) {
// 2x3 2차원 배열 리팩터링 - new 생략 가능
int[][] arr = new int[2][3];
int num = 1;
for(int row = 0; row < arr.length; row++) {
for (int column = 0; column < arr.length; column++) {
arr[row][column] = num++;
}
}
// 리팩터링
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();
}
}
}