2019. 11. 13. 13:50 카테고리 없음
java 배열 기본 사용법
int[] score = new int[]{50, 60, 70, 80, 90};
int[] socre = {50, 60, 70, 80, 90}; //new int[]를 생략 가능함.
int[] score;
score = new int[]{50, 60, 70, 80, 90}; // OK
score = {50, 60, 70, 80, 90}; // error -> new int[] 생략 불가
배열을 간단히 출력하는 방법으로는
System.out.println(Arrays.toString(score)); 와 같이 출력하면 된다
만약에 System.out.println(score); 처럼 출력을 하려고 시도하였다면 배열의 주소가 출력되었을 것이다.
다만 예외적으로 char 배열은 println메서드로 출력하면 각 요소가 구분자 없이 그대로 출력된다
char[] chArr = {'a', 'b', 'c', 'd'};
System.out.println(chArr); // abcd가 출력된다
마찬가지로 배열로 출력하려면
System.out.println(Arrays.toString(chArr));를 사용하면 된다.,