学习数组atm(自以为是的菜鸟)有人可以解释一下这部分代码是什么意思吗?
for (int answer = 0; answer < responses.length; answer++ )
++frequency[ responses[ answer ] ];
我无法理解它的逻辑。显然“答案”是我的计数器。频率增加 1 直到它到达数组 #11(即数字 10),但括号内发生的事情让我感到困惑。但如果达到最大数量 10,结果(正确)给出实际频率。我的猜测是这段代码操纵了这个类的很大一部分。
我建立在这段代码之上,但我默认使用这部分代码(因为它本身就在书中给出)。
谢谢你。
这是完整的代码
public class StudentPoll
{
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, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6,
4, 8, 6, 8, 10};
int[] frequency = new int[ 11 ];
for (int answer = 0; answer < responses.length; answer++ )
++frequency[ responses[ answer ] ];
System.out.printf( "%s%10s\n", "Rating", "Frequency" );
for ( int rating = 1; rating < frequency.length; rating++ )
System.out.printf( "%d%10d\n", rating, frequency[ rating ] );
}
}