JAVA/배열

배열

꼬투리 2022. 7. 11. 11:30

배열 : 같은 타입의 여러 변수를 하나의 묶음으로 다루는 것

 

배열의 선언 - 배열을 다루기 위한 참조변수의 선언

int score[];
String name[];
int[] score = new int[5];

int[] score;
String[] name;

배열의 저장공간은 연속적이라는 특징이 있다.

 

배열의 선언은 위와 같이 두가지 방법으로 선언이 가능하다.

 

1. 타입[] 변수이름; (Java 스타일) // 배열의 선언

변수이름 = new 타입[길이]; // 배열의 생성( 실제 저장공간을 생성)

 

2.타입 변수이름[]; (C언어 스타일)

 

 

배열의 인덱스

-> 각 요소에 자동으로 붙는 번호

 

int[] score = new int[5]; -> 인덱스는 0~4까지 존재

 

public class arrEx_1 {

	public static void main(String[] args) {

		
		
		
//		int[] score;//1. 배열 score를 선언(참조변수)
//		score = new int[5]; //2. 배열의 생성(int 저장공간 * 5);
		
		
//		선언과 생성을 동시에
		int[] score = new int[5];
		score[3] = 100;
		
		System.out.println("score[0]="+score[0]);
		System.out.println("score[1]="+score[1]);
		System.out.println("score[2]="+score[2]);
		System.out.println("score[3]="+score[3]);
		System.out.println("score[4]="+score[4]);
		
		int value = score[3];
		System.out.println("value ="+value);
		
		
		
		
	}

}