调用一个方法 n 次:我应该使用转换后的 for-each 循环还是传统的 for 循环?

发布于 2024-08-30 08:25:59 字数 620 浏览 9 评论 0原文

考虑到需要循环到任意 int 值,将值转换为数组并 for-each 数组是更好的编程实践,还是仅使用传统的 for 循环?

仅供参考,我正在计算 6 面骰子多次投掷中 5 和 6 结果(“命中”)的数量。我的任意 int 值是 dicePool ,它表示多次抛出的次数。

据我了解,有两个选项:

  1. 将 dicePool 转换为数组并 for-each 数组:

    public int calcHits(int dicePool) {
       int[] dp = new int[dicePool];
       for (整数 a : dp) {
         // 调用 throwDice 方法
       }
    }
    
  2. 使用传统的 for 循环:

    public int calcHits(int dicePool) {
       for (int i = 0; i < dicePool; i++) {
         // 调用 throwDice 方法
       }
    }
    

我的观点是,选项 1 是笨拙的代码,并且涉及不必要的数组创建,尽管 for-each 循环比选项 2 中的传统 for 循环更有效。

Given the need to loop up to an arbitrary int value, is it better programming practice to convert the value into an array and for-each the array, or just use a traditional for loop?

FYI, I am calculating the number of 5 and 6 results ("hits") in multiple throws of 6-sided dice. My arbitrary int value is the dicePool which represents the number of multiple throws.

As I understand it, there are two options:

  1. Convert the dicePool into an array and for-each the array:

    public int calcHits(int dicePool) {
       int[] dp = new int[dicePool];
       for (Integer a : dp) {
         // call throwDice method
       }
    }
    
  2. Use a traditional for loop:

    public int calcHits(int dicePool) {
       for (int i = 0; i < dicePool; i++) {
         // call throwDice method
       }
    }
    

My view is that option 1 is clumsy code and involves unnecessary creation of an array, even though the for-each loop is more efficient than the traditional for loop in Option 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雅心素梦 2024-09-06 08:25:59

此时,速度并不重要(插入过早优化注释;)。 重要的是您能够多快地理解代码的作用,即调用方法 dicePool 次。

第一个方法分配一个大小为 dicePool 的数组并迭代其值,这恰好运行循环体 dicePool 次(我假设你的意思是 int< /code> 而不是 Integer 以避免不相关的自动装箱问题)。这对于运行代码的计算机来说可能效率低下,但更重要的是,对于人类阅读代码来说效率低下,因为它在概念上与您想要完成的目标相距甚远。具体来说,您迫使读者考虑刚刚创建的新数组以及变量 a 的值,该值对于循环的每次迭代都将为 0,即使这些都不是与你的最终目标相关。

任何查看第二种方法的 Java 程序员都会意识到,您正在执行循环体 dicePool 次,同时 i '计数'到 dicePool。虽然后一部分并不是特别重要,但开始正是您想要做的。使用这个常见的 Java 惯用法可以最大限度地减少读者需要考虑的不相关的事情,因此它是最好的选择。

如有疑问,请以简单的方式进行。 :D

At this point, speed isn't important (insert premature-optimization comment ;). What matters is how quickly you can understand what the code does, which is to call a method dicePool times.

The first method allocates an array of size dicePool and iterates through its values, which happens to run the loop body dicePool times (I'll pretend you meant int instead of Integer to avoid the unrelated autoboxing issue). This is potentially inefficient for the computer running the code, but more importantly it's inefficient for the human reading the code as it's conceptually distant from what you wanted to accomplish. Specifically, you force the reader to think about the new array you've just made, AND the value of the variable a, which will be 0 for every iteration of the loop, even though neither of those are related to your end goal.

Any Java programmer looking at the second method will realize that you're executing the loop body dicePool times with i 'counting up' to dicePool. While the latter part isn't especially important, the beginning is exactly what you meant to do. Using this common Java idiom minimizes the unrelated things a reader needs to think about, so it's the best choice.

When in doubt, go with simplicity. :D

友谊不毕业 2024-09-06 08:25:59

为什么需要分配一个数组来循环一个可以安全递增和使用而无需分配的变量?

这听起来效率很低。如果您需要交换整数的顺序,则可能需要分配一个数组,但事实并非如此。我肯定会选择选项2

当您想要迭代集合时,foreach 非常有用,但创建一个集合只是为了迭代它(当您不需要它时)只是没有意义..

Why would you need to allocate an array to loop over a variable that can be safely incremented and used without any need of allocation?

It sounds unecessarily inefficient. You can need to allocate an array if you need to swap the order of ints but this is not the case. I would go for option 2 for sure.

The foreach is useful when you want to iterate on a collection but creating a collection just to iterate over it when you don't need it is just without sense..

別甾虛僞 2024-09-06 08:25:59

(2) 是显而易见的选择,因为根据您的描述,创建数组没有意义。如果有的话,情况当然会改变。

(2) is the obvious choice because there's no point in creating the array, based on your description. If there is, of course things change.

定格我的天空 2024-09-06 08:25:59

是什么让您认为 for-each 循环效率更高?

迭代集合的效率很可能低于简单的循环和计数器。

如果您提供有关该问题的更多上下文,特别是这个问题是否比选择一种语法而不是另一种语法更重要,可能会有所帮助。我无法想出#1 是更好的解决方案的问题。

What makes you think that the for-each loop is more efficient?

Iterating over a set is very likely less efficient than a simple loop and counter.

It might help if you gave more context about the problem, specifically whether there's more to this question than choosing one syntax over the other. I am having trouble thinking of a problem to which #1 would be a better solution.

当爱已成负担 2024-09-06 08:25:59

我不会写第一个。没有必要在每个设置中都使用最新的语法。

你的直觉是好的:如果感觉和看起来很笨拙,那么它可能就是这样。

跟着#2 去,晚上睡觉。

I wouldn't write the first one. It's not necessary to use the latest syntax in every setting.

Your instinct is a good one: if it feels and looks clumsy, it probably is.

Go with #2 and sleep at night.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文