Java & Android
Java 배열
그레이트쪼
2016. 9. 10. 21:36
배열의 생성과 초기화
- 자바에서 배열은 객체다.
- 배열을 선언하는 것을 보자
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // A basic type of declaration. // false for boolean array, 0 for int array, // and null for Object array are default values. // They are set automatically when the arrays are initialized. int [] intArray = new int[4]; // Particular numbers are assigned when an array is declared. int [] intArray = {1, 2, 3, 4}; // Of course, declaration and assignment can be separated. int [] intArray; // null is assined to this array at this moment // because array is also an object. intArray = new int[] {1, 2, 3, 4}; // Declaration, object creation, and assignment are all separated. boolean [] boolArray; // null is assined to this array at this moment // because array is also an object. boolArray = new boolean[4]; // the address of the object is assigned to the variable Arrays.fill(boolArray, true); // Arrays.fill() is used // when the one value is assigned to all elements. |
- 배열은 객체이므로 변수에는 null이 들어가거나 객체의 주소가 들어간다.
- 생성만 하면 초기값이 자동으로 할당되는 것에 주의하자.
2차원 배열
- for-each를 이용하여 iteration 하기
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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); } } |