Language/Java

7. Java 배열

유가엘 2020. 3. 5. 17:38

lesson07_Array.zip
0.01MB

배열이란

다수의 자료형이 같은 데이터를 인덱스를 통해 관리하는 것

배열은 0번부터 시작합니다.

 

배열의 단점

데이터 종류를 섞지 못함( String, int .. )
만들때 몇칸짜리인지 알아야합니다.
칸수 및 위치를 수정할 수 없습니다.

 

배열의 메모리 크기

배열을 구성하는 데이터의 자료형에 따라 메모리 크기가 결정되고,

객체 자료형에 속하므로, 배열 변수안에는 데이터 주소가 담겨있습니다.

만약 배열 선언 후 null을 담는다면, 메모리상에는 공간을 차지하지만 내용은 담기지 않습니다.

int[] Array1 = {10,20,30,40};  메모리 공간이 할당되며, Array1에는 값을 향한 주소가 담겨있습니다.

int[] Array2 = null;              메모리 공간이 할당되며, Array2에 값을 향한 주소가 담겨있습니다. 단, 실제 값은 없습니다.

 

배열 생성의 구조

//		int배열 객체를 생성 후, intValue[1]안에 값을 넣었습니다.
//		int는 4byte이며 3개를 배열 시켰으므로, intValue는 총 12byte입니다.
		int[] intValue = new int[3];	
		intValue[1] = 1; 
		System.out.println("intValue[0]의값\t" + intValue[0]);
		System.out.println("intValue[1]의값\t" + intValue[1]);
		System.out.println("intValue[2]의값\t" + intValue[2]);
		System.out.println("-----------------------------------");
//		string배열 객체 생성시, Hello를 넣어줍니다.
//		string은 4byte이며 3개를 배열 시켰으므로, stringValue는 총 12byte입니다.
		String stringValue[] = {"Hello","Java","World"};
		System.out.println("stringValue[0]의값\t" + stringValue[0]);
		System.out.println("stringValue[1]의값\t" + stringValue[1]);
		System.out.println("stringValue[2]의값\t" + stringValue[2]);
		System.out.println("stringValue배열의크기\t" + stringValue.length);
		System.out.println("-----------------------------------");

 

다차원 배열

배열안에 또다른 배열이 존재하는 형태입니다.

		String doublueArray[][]= {{"안","녕"},{"하","세","요"}};
		System.out.println(doublueArray[0][0]);
		System.out.println(doublueArray[0][1]);
		System.out.println(doublueArray[1][0]);
		System.out.println(doublueArray[1][1]);
		System.out.println(doublueArray[1][2]);
		System.out.println("-----------------------------------");