Java

배열 선언, 초기화

hs_developer 2022. 6. 24. 20:18

배열은 값을 담을 수 있는 칸을 여러 개 파는 것.

 

public class ArrayTest {

	public static void main(String[] args) {
		
		int[] arrCase1= new int[3];
		
		arrCase1[0]= 10; // 배열에 값 넣어 초기화
		System.out.println("arrCase1[0]: " + arrCase1[0]); // 10
		
		String[] arrCase2= {"A", "B", "C"}; // 선언과 동시에 초기화
		
		System.out.println("arrayCase2[0]: " + arrCase2[0]); // A
		System.out.println("arrayCase2 Length: " + arrCase2.length); // 3
	}
}

 

배열의 데이터를 숫자로 선언 시 초기값은 모두 0이며, 문자일 경우에는 모두 null이다.

 

배열은 특정 데이터 타입을 칸을 여러 개 파는 것이고

 

한 번 크기를 선언하면 이 후 변경이 불가능하다.

 

배열은 크기만 넣어 선언한 후 초기화 시키거나 

int[] arrCase= new int[3];
arrCase[0]= 10;

 

선언과 동시에 초기화 시키는 것도 가능하다.

int arr[]= {1, 2, 3};

 

 

 

참고

https://wakestand.tistory.com/notice/104

'Java' 카테고리의 다른 글

extends, implements  (0) 2022.06.24
List, Set, Map  (0) 2022.06.24
static 변수, static 메서드  (0) 2022.06.22
제네릭  (0) 2022.06.13
컬렉션 프레임워크  (0) 2022.06.10