数组的预增量(Pre increment on arrays)
任何人都可以解释这段代码的输出吗? 我一直很难理解,但我只是不明白。
public static void main(String ars[]) { int responses[] = {1,2,4,4}; int freq[] = new int[5]; for(int answer = 1; answer < responses.length; answer++) { ++freq[responses[answer]]; } for (int rating = 1; rating < freq.length; rating++) System.out.printf("%6d%10d\n", rating, freq[rating]); }产量
1 0 2 1 3 0 4 2Can anyone explain the output of this code? I have been hitting my head really hard to understand, but I just don't get it.
public static void main(String ars[]) { int responses[] = {1,2,4,4}; int freq[] = new int[5]; for(int answer = 1; answer < responses.length; answer++) { ++freq[responses[answer]]; } for (int rating = 1; rating < freq.length; rating++) System.out.printf("%6d%10d\n", rating, freq[rating]); }Output
1 0 2 1 3 0 4 2最满意答案
我尝试了一些简单的事情,所以你可以看到发生了什么:
int responses[] = new int[4]; responses[0] = 1; responses[1] = 2; responses[2] = 4; responses[3] = 4; int freq[] = new int[5]; for(int answer = 1; answer < 4; answer++) { int x = responses[answer]; freq[x] = freq[x] + 1; } for (int rating = 1; rating < 5; rating++) { //Print 6 spaces and then the rating variable //Print 10 spaces then the integer at freq[rating] System.out.printf("%6d%10d\n", rating, freq[rating]); }我会查找++前缀和后缀 。
I've tried to simply things a bit so you can see what is going on:
int responses[] = new int[4]; responses[0] = 1; responses[1] = 2; responses[2] = 4; responses[3] = 4; int freq[] = new int[5]; for(int answer = 1; answer < 4; answer++) { int x = responses[answer]; freq[x] = freq[x] + 1; } for (int rating = 1; rating < 5; rating++) { //Print 6 spaces and then the rating variable //Print 10 spaces then the integer at freq[rating] System.out.printf("%6d%10d\n", rating, freq[rating]); }I would look up the ++ prefix & postfix.
更多推荐
发布评论