我的Java数组中的增量(The increment in my Java array)

编程入门 行业动态 更新时间:2024-10-28 12:18:16
我的Java数组中的增量(The increment in my Java array)

有人可以解释下面我给定程序中的++freq是什么吗?

package array; import java.util.Random; public class CounterArray { public static void main(String[] args){ Random Ran = new Random(); int freq[] = new int [7]; for(int roll=1;roll<100;roll++){ ++freq[1+Ran.nextInt(6)]; } System.out.println("face\tFrequency"); for(int face=1;face<freq.length;face++){ System.out.println(face+"\t"+freq[face]); } } }

Can someone explain what does ++freq in my given program below?

package array; import java.util.Random; public class CounterArray { public static void main(String[] args){ Random Ran = new Random(); int freq[] = new int [7]; for(int roll=1;roll<100;roll++){ ++freq[1+Ran.nextInt(6)]; } System.out.println("face\tFrequency"); for(int face=1;face<freq.length;face++){ System.out.println(face+"\t"+freq[face]); } } }

最满意答案

该程序所做的是“掷骰子”99次(闻起来像一个“一个一个”的错误)并计算每个数字被发现的频率。 阵列freq在位置i保持i切割的freq 。

有问题的代码生成下一个随机数,然后递增数组中的相应插槽。

++freq[1 + Ran.nextInt(6)];

如果我们分开它会变得更加清晰。

首先,评估Ran.nextInt(6) ,其产生来自集合{0,1,2,3,4,5}的随机整数。 为此,添加1以从集合{1,2,3,4,5,6}中获得数字。 让我们将这个中间结果存储在一个单独的变量中以获得清晰度: int result = Ran.nextInt(6) + 1 。 接下来,在阵列中查找相应的频率并递增1。 ++freq[result]与freq[result] += 1具有相同的效果。

数组freq的长度为7,因此可以使用“自然”索引。 也就是说,元素freq[0]被浪费了。 (我怀疑这是非常好的风格。)

关于风格的另一个词:人们普遍接受的做法是为类型保留资本名称而不是将它们用于变量,所以Ran应该真的命名为ran以避免混淆。

另外一个补充: i++的后增量运算符和++i的预增量运算符之间的区别在于前一个表达式的结果是增量的i值,而后者的值是值增量后。 在你的情况下,它没有任何区别,因为你还没有使用结果。 要查看其工作原理,请尝试运行以下代码:

int i = 10; System.out.printf("i = %d%n", i); System.out.printf("i++ = %d%n", i++); System.out.printf("i = %d%n", i); System.out.printf("++i = %d%n", ++i); System.out.printf("i = %d%n", i);

What the program does is “rolling a dice” 99 times (which smells like an “off by one” error) and count how often each number was found. The array freq holds at position i how often i was diced.

The code in question generates the next random number and then increments the respective slot in the array.

++freq[1 + Ran.nextInt(6)];

It will become more clear if we break it apart.

First, Ran.nextInt(6) is evaluated which yields a random integer from the set {0, 1, 2, 3, 4, 5}. To this, 1 is added to get a number from the set {1, 2, 3, 4, 5, 6}. Let's store this intermediate result away in a separate variable for clearness: int result = Ran.nextInt(6) + 1. Next, the respective frequency is looked up in the array and incremented by one. ++freq[result] which has the same effect as freq[result] += 1.

The length of the array freq was made to be 7 so the “natural” index can be used. That is, the element freq[0] is wasted. (I doubt that this is very good style.)

Another word on style: It is generally accepted practice to reserve capital names for types and not use them for variables, so Ran should really be named ran to avoid confusion.

One further addition: The difference between the post-increment operator as in i++ and the pre-increment operator as in ++i is that the result of the former expression is the value of i before the increment while that of the latter is the value after the increment. In your case, it makes no difference because you are not using the result anyway. To see how this is working, try running the following code:

int i = 10; System.out.printf("i = %d%n", i); System.out.printf("i++ = %d%n", i++); System.out.printf("i = %d%n", i); System.out.printf("++i = %d%n", ++i); System.out.printf("i = %d%n", i);

更多推荐

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

发布评论

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

>www.elefans.com

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