// 데이터타입[] 참조변수 = new 데이터타입[배열크기]
int[] arr1 = new int[10]
배열의 참조변수를 이용하면 배열객체가 생성된 메모리까지 접근할 수 있다.
public class ArrayTest {
public static void main(String[] args) {
int[] scoreList = new int[5];
scoreList[0] = 76;
scoreList[1] = 92;
scoreList[2] = 49;
System.out.println("scoreList[0] : " + scoreList[0]); // 76출력
System.out.println("scoreList[1] : " + scoreList[1]); // 92출력
System.out.println("scoreList[2] : " + scoreList[2]); // 49출력
}
}
배열객체를 생성하면서 동시에 초기화까지 처리할 수있다.
int[] scoreList = {76,92,49,83,100);
일반적으로 배열에 들어있는 값들을 순차적으로 처리할때는 for문이나 향상된 for문을 사용한다.
<for문 사용>
public class ArrayTest4 {
public static void main(String[] args) {
int[] scoreList = {76, 92, 49, 83, 100};
for (int i = 0; i < 5; i++) {
System.out.println("scoreList[" + i + "] : " + scoreList[i]);
}
}
}
<향상된 for문 사용>
public class ArrayTest4 {
public static void main(String[] args) {
int[] scoreList = {76, 92, 49, 83, 100};
// for (int i = 0; i < 5; i++) {
// System.out.println("scoreList[" + i + "] : " + scoreList[i]);
// }
for (int score : scoreList) {
System.out.println("score : " + score);
}
}
}
length 변수는 배열객체의 길이를 확인할때 사용한다.
public class ArrayTest4 {
public static void main(String[] args) {
int[] scoreList = {76, 92, 49, 83, 100};
System.out.println(scoreList.length); // 배열의 길이인 5가 출력된다.
}
}
배열은 형변환이 가능한 경우 다른 타입의 데이터도 저장할 수 있다.
2차원배열은
데이터타입[행][렬] 배열이름 이라고 생각하면 된다.

데이터타입[][] 배열변수
int[][] scoreList