Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 763799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:37:14+00:00 2026-05-14T16:37:14+00:00

Given the need to loop up to an arbitrary int value, is it better

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T16:37:15+00:00Added an answer on May 14, 2026 at 4:37 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try array literal, it worked for me (tested with success… May 16, 2026 at 6:35 pm
  • Editorial Team
    Editorial Team added an answer Try myMutableHashMap((x, y)) = z. In fact, you don't need… May 16, 2026 at 6:35 pm
  • Editorial Team
    Editorial Team added an answer The Group UID 99 and Owner UID 99 is Nobody… May 16, 2026 at 6:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a query that I need to loop. query=select '$dbserver' as server; while
I need to copy data from one database to another using a VB.NET program.
Given two orthogonal unit vectors, A and B, and two different orthogonal unit vectors
I have a vector of points, and I need to get those which are
So I want to start and say I am new to Java programming. Here
here is my problem I have the following array (for example) string[] arr =
I'm working on a graphical application which looks something like this: while (Simulator.simulating) {
I have been trying to solve this problem for a while, but couldn't with
I have written a simple brainfuck interpreter in MATLAB script language. It is fed
Is it possible to access the previous element generated in a list comprehension ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.