- 자바에서 배열은 객체다.
- 배열을 선언하는 것을 보자
// 보통은 아래와 같이 선언과 생성을 한다.
// Java에서는 기본적으로 boolean array의 경우 false,
// int array의 경우 0, Object array의 경우 null로 자동 초기화된다.
int [] intArray = new int[4];
// 특정 값을 대입하면서 배열을 선언하였다.
int [] intArray = {1, 2, 3, 4};
// 물론 선언과 생성&대입을 분리할 수도 있다.
int [] intArray; // 배열도 객체이므로 이 경우 null이다.
intArray = new int[] {1, 2, 3, 4};
// 이번엔 선언과 생성, 대입을 모두 분리해보자.
boolean [] boolArray; // 객체이므로 이 경우 null이다.
boolArray = new boolean[4]; // 이 시점에 객체의 주소가 들어가게 된다.
Arrays.fill(boolArray, true); // 일괄적으로 값을 대입할 때 Arrays.fill()을 사용한다.
- 배열은 객체이므로 변수에는 null이 들어가거나 객체의 주소가 들어간다.
- 생성만하면 초기값이 자동으로 할당되는 것에 주의하자.
2차원 배열
- for-each를 이용하여 iteration 하기
String properties[][] = {
{"application_id", "text"}, {"sample_time", "integer"},
};
for (String[] propertyInfo : properties) {
System.out.println(propertyInfo[0] + ": " + propertyInfo[1]);
}
// or
for (String[] propertyInfo : properties) {
for (String detail : propertyInfo) {
System.out.println(detail);
}
}
댓글 없음:
댓글 쓰기