배열은 같은 타입의 변수를 사용하기 편하게 하나로 묶어둔 것
int [] students; //배열 변수를 선언
students = new int [5]; //배열 생성
//변수 값 대입
students[0] = 90;
students[1] = 80;
students[2] = 70;
students[3] = 60;
students[4] = 50;
//변수 값 사용
System.out.println("학생1 점수: " + students[0]);
System.out.println("학생2 점수: " + students[1]);
System.out.println("학생3 점수: " + students[2]);
System.out.println("학생4 점수: " + students[3]);
System.out.println("학생4 점수: " + students[4]);
int [] studens; //배열 변수 선언
student = new int [5]; //배열 생성


int[] students = new int[5]; //1. 배열 생성
int[] students = x001; //2. new int[5]의 결과로 x001 참조값 반환
students = x001 //3. 최종 결과
배열은 변수와 사용 방법이 비슷한데, 차이점이 있다면 다음과 같이 [] 사이에 숫자 번호를 넣어주면 된다! 배열의 위치를 나타내는 숫자를 인덱스 (index) 라고 한다.

new int [5]와 같이 5개의 요소를 가지는 int형 배열을 만들었다면, 인덱스는 0,1,2,3,4가 존재한다. 여기서 주의해야 할 점은 인덱스는 0부터 시작한다는 점!
배열의 요소를 5개를 생성했지만, 인덱스는 0부터 시작이 된다! 따라서 사용이 가능한 인덱스의 범위는 0~(n-1)이 된다
배열에 값을 대입하던, 배열의 값을 사용하든 간에 일반적인 변수와 사용법은 같다
System.out.println("학생1 점수: " + students[0]);
// 변수에 있는 참조값을 통해 실제 배열에 접근. 인덱스를 사용해서 해당 위치의 요소에 접근
System.out.println("학생1 점수: " + x001[0]);
// 배열의 값을 읽어옴
System.out.println("학생1 점수: " + 90)
배열을 사용하면, 이렇게 참조값을 통해서 실제 배열에 접근하고, 인덱스를 통해서 원하는 요소를 찾는다.
기본형은 선언과 동시에 크기가 정해진다 즉 동적으로 바꾸거나 할 수 없다.
반면, 배열과 같은 참조형은 크기를 동적으로 할당할 수 있다.
System.out.println("학생1 점수: " + students[0]);
System.out.println("학생2 점수: " + students[1]);
System.out.println("학생3 점수: " + students[2]);
System.out.println("학생4 점수: " + students[3]);
System.out.println("학생4 점수: " + students[4]);
여기서 변수 명이 students로 같기 때문에, 숫자가 반복되는 부분만 해결되면 for문을 사용하면 될 것 같다!
리펙토링(Refactoring)은 기존의 코드 기능은 유지하면서 내부 구조를 개선하여 가독성을 높이고, 유지보수를 용이하게 하는 과정을 말한다.
중복 제거, 복잡성을 줄이고, 이해하기 쉬운 코드로 만들기 위해서 수행된다.
package array;
public class Array1Ref2 {
public static void main(String[] args) {
int[] students; //배열 변수 선언
students = new int[5]; //배열 생성
//변수 값 대입
students[0] = 90;
students[1] = 80;
students[2] = 70;
students[3] = 60;
students[4] = 50;
for (int i = 0 ; i <students.lenght; i ++){
System.out.println("학생" + (i + 1) + "점수 : " + students[i]);
}
int [] students;
int [] students = new int [] {90,80,70,60,50};
package array;
public class Array1Ref3 {
public static void main(String[] args) {
int[] students;
students = new int[]{90, 80, 70, 60, 50}; //배열 생성과 초기화
for (int i = 0; i < students.length; i++) {
System.out.println("학생" + (i + 1) + " 점수: " + students[i]);
}
}
}
int [] students = new int[]{90,80,70,60,50}; 으로 한줄로 사용 가능함.
int [] students = {90, 80, 70, 60, 50};
와 같이 {}만 사용해서 생성과 동시에 편리하게 초기화 하는 기능을 제공한다.
package array;
public class Array1Ref4 {
public static void main(String[] args) {
int [] students = {90,80,70,60,50};
for (int i = 0; i <students.length; i ++) {
System.out.println("학생+ + (i + 1)+ "점수 : " + students[i]);
}
}
}
배열을 사용해 프로그램을 전체적으로 잘 구조화 함.
2차원 배열은 행과 열로 구성이 된다.

2차원 배열은 int[][] arr = new int [2][3]와 같이 선언하고 생성한다.
그리고 arr[1][2]와 같이 사용하는데, 행을 먼저 찾고, 열번호를 찾는다.

코드를 살펴보면 비슷한 부분이 계속 반복이 된다.


2차원 배열의 초기화는 밖이 행이 되고, 안이 열이 된다.
int [][] arr = {
{1,2,3},
{4,5,6}
};
배열의 길이

for 문에서 2차원 배열의 길이를 활용해보자
배열의 크기와 상관 없이 배열에 순서대로 1씩 증가하는 값을 입력하도록 변경해보자
int [][] arr = new int [3][4]; //3행 4열
int i = 1;
for (int row = 0; row < arr.lenght < row ++){
for (int column = 0; column < arr[row].lenght < column ++){
arr[row][column] = i ++;
}
}
int [] numbers = {1,2,3,4,5};

배열에 있는 값을 순서대로 읽어서 number 변수에 넣고 출력한다.
배열의 값을 읽으려면 int i와 같은 인덱스를 탐색할 수 있는 변수를 선언해야 함.
또한 i <numbers.lenght와 같이 배열의 끝 조건을 지정해주어야 함.

향상된 for문에는 증가하는 인덱스 값이 감추어져 있다. 따라서 int i 와 같은 증가하는 인덱스 값을 직접 사용해야 하는 경우에는 향상된 for문을 사용할 수 없다
