数组的预增量(Pre increment on arrays)

编程入门 行业动态 更新时间:2024-10-10 12:26:33
数组的预增量(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 2

Can 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.

更多推荐

本文发布于:2023-07-30 07:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1337114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   预增   Pre   increment   arrays

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!