동일한 타입의 데이터를 연속된 공간에 나열하고, 각 데이터에 인덱스를 부여하여 하나의 변수에 저장해 관리할 수 있는 자료 구조
Java에서 배열은 객체 번지를 참조하는 참조 타입으로 간주된다. 따라서 객체의 특성을 가지며 객체로서 다룰 수 있다.
new 연산자의 동작으로 생성된다.Object 클래스로부터 상속받는 메서드들을 사용할 수 있다(equals(), hashCode(), toString()).length 속성을 가진다.new 연산자를 통해 배열의 크기를 지정할 때 드러난다.배열의 길이는 불변하지만, 배열 내부의 요소 값은 가변적이다.
int[] myArray;
Book[] book;new 연산자를 사용하며, 배열의 크기를 지정해야 한다.myArray = new int[10];
book = new Book[3];
System.out.println(myArray.length); // 10
System.out.println(book.length); // 3배열이 생성되면, 각 요소들은 타입에 따라 기본값으로 저장된다.myArray[0] = 3;
// ...
myArray[9] = 10;
// myArray[10] = 9; -> IndexOutOfBoundsException
int[] myArrayFirst = new int[]{1, 2, 3};
int[] myArraySecond;
myArraySecond = new int[]{1, 2, 3};
int[] myArrayThird = {1, 2, 3};
int[] myArrayFourth;
// myArrayFourth = {1, 2, 3};는 안 됨. 한 줄에 쓰는 것만 가능

a.length; // 2
a[0].length; //4
a[2].length; //4
System.out.println(a); // [[I@5a61f5df
System.out.println(a[0]); // [I@3551a94
System.out.println(a[1]); // [I@531be3c5
int[][] b = {
{1,2,3},
{4,5,6},
{7,8,9},
}
int[][] c = new int[3][]; // 열의 개수 비워두기
c[0] = new int[2];
c[1] = new int[1];
c[2] = new int[5];
char[][] df = new char[5][];
for(int i = 0; i < df.length; i++){
df[i] = new char[i+1];
for(int j = 0; j < df[i].length; j++){
df[i][j] = '*';
System.out.print(df[i][j]);
}
System.out.println();
}
/*
*
**
***
****
*****
*/