배열 생성
int[] intArray = new int[5];
int[] intArray;
intArray = new int[5];
int[] intArray = {1, 2, 3, 4, 5};
int[] intArray;
intArray = {1, 2, 3, 4, 5};
배열 접근
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
intArray[3] = 4;
intArray[4] = 5;
System.out.println(intArray[0] + intArray[1]);
String[] fruitsArray = new String[5];
fruitsArray[0] = "딸기";
fruitsArray[1] = "당근";
fruitsArray[2] = "수박";
fruitsArray[3] = "참외";
fruitsArray[4] = "메론";
for (String fruit : fruitsArray) {
System.out.println(fruit);
}
앨리어싱(Aliasing)
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = arr1;
arr1[0] = 100;
System.out.println(arr2[0]);
깊은 복사 - arr1.clone()
int[] arr1 = {1, 2, 3, 4, 5};
int[] arr2 = arr1.clone();
arr1[0] = 100;
System.out.println(arr1[0]);
System.out.println(arr2[0]);
다중 배열
int[][] multiArray;
int[][] multiArray = new int[3][4];
int[][] multiArray = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
for (int i = 0; i < multiArray.length; i++) {
for (int j = 0; j < multiArray[i].length; j++) {
multiArray[i][j] = (i * 4 + 1) + j;
}
}