세인트킴 2023. 5. 6. 17:19
public class ex06_3 {
	public static void main(String[] args) {
		int[] responses = {1, 2, 6, 4, 8, 5, 9, 7, 8, 10,
				1, 6, 3, 8, 6, 10, 3, 8, 2, 7,
				6, 5, 7, 6, 8, 7, 5, 6, 6, 5,
				6, 7, 5, 6, 4, 8, 6, 8, 10, 6};

		int[] a = new int[11];
		System.out.println("Rating  Frequency");
		for(int i=0; i<responses.length; i++) {
			++a[responses[i]];
		}
		for(int j=1; j<11; j++) {
			System.out.printf("%6d %10d\n", j, a[j]);
	}
		}
	}

a[j]를 하는 이유는 배열의 인덱스를 표현할 때, 대괄호([])안에 인덱스 값을 지정하는 것이 일반적인 방법이다.

a[j]형태로 표현하는 것은 배열 a의 j번째 요소를 의미하고, j[a]는 배열 a가 아닌 j라는 이름의 배열에서 a번째 요소를 의미하는 것이다.