본문 바로가기
프로그래밍/자바의정석_5_연습문제

[5-1] 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명하시오.

by 므시칸곰틔군 2015. 1. 5.

public class Ex5_1 {


// [5-1] 다음은 배열을 선언하거나 초기화한 것이다. 잘못된 것을 고르고 그 이유를 설명

// 하시오.

// a. int[] arr[];

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

// c. int[] arr = new int[5];

// d. int[] arr = new int[5]{1,2,3,4,5};

// e. int arr[5];

// f. int[] arr[] = new int[3][];

//


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

int[] arr1[];

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

int[] arr3 = new int[5];

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

int arr5[] = new int[5];

int[] arr6[] = new int[3][];


}

// [5-1] 다음은 배열을 선언하거나 초기화 한 것이다. 잘못된 것을 고르고 그 이유를 설명

// 하시오.

// a. int[] arr[];

// b. int[] arr = {1,2,3,}; // 마지막의 쉼표‘,’는 있어도 상관없음.

// c. int[] arr = new int[5];

// d. int[] arr = new int[5]{1,2,3,4,5}; // 두 번째 대괄호[]에 숫자 넣으면 안됨.

// e. int arr[5]; // 배열을 선언할 때는 배열의 크기를 지정할 수 없음.

// f. int[] arr[] = new int[3][];

// [정답] d, e

// [해설] d. int[] arr = new int[]{1,2,3,4,5}에서는 대괄호[]안에 배열의 크기를 지정할

// 수 없다. 괄호{}안의 데이터의 개수에 따라 자동적으로 결정되기 때문이다.

}