0

I'm making a yahtzee game in java, and I have a variable called frequencyCount which is an array keeping track of how many die there is of each one. I reset this array after each roll with a for loop setting each index to 0.

for(int i = 0; i < frequencyCount.length; i++) {
    frequencyCount[i] = 0;
}

But is there any problem with just creating a new array each time like this:

frequencyCount = new int[6];

The reset method will be called 45 times per player per game.

Which way is best? Is it a problem to keep creating new arrays, or doesn't it matter since this is a small game?

5
  • 2
    A modern Java VM can create millions of arrays without you even noticing. 45 and 6 are tiny tiny numbers for a computer. 99.99999% of the time spent in your program will consist in doing nothing, and wait for the user interactions. As long as no other object has a reference to the old array, resetting it or creating a new one won't change anything. Commented Jan 26, 2014 at 17:17
  • Note that you can also use Arrays.fill(frequencyCount, 0); (which just saves you from writing the loop yourself). Commented Jan 26, 2014 at 17:24
  • @JBNizet It can create millions of small arrays, large (thousands, millions of elements) arrays are a different matter. And many important programs don't spend the majority (let alone 99.99999%) of their time waiting for user input. Even those who do may have latency constraints that make optimizations worthwhile. Not to condone premature optimization, but I prefer to dismiss these concerns for right reasons. Commented Jan 26, 2014 at 17:26
  • 1
    I would recommend dispensing with using arrays entirely. Use the ArrayList implementation of the List interface - it has a very convenient clear() method. Commented Jan 26, 2014 at 17:32
  • @delnan: we're talking about 45 creations of arrays of length 6 here, for a Yathzee program. The same optimization rules don't apply for such a program and a low-latency high-volume trading application. My point is to make the OP realize that a computer is fast. Much faster than he can imagine. Commented Jan 26, 2014 at 17:32

3 Answers 3

5

In practice you will not see any difference, you shouldn't worry too much about performance in cases like this. Focus on keeping your code clean and readable, and I believe method no. 2 is more concise.

Sign up to request clarification or add additional context in comments.

Comments

1

both ways are almost identical, new int[] also uses for loop to make all of them zero (optimized) but will throw the old array and make the GC work, so both ways almost identical

Comments

0

I agree with Dima; both methods produce the same result. I would say to use the second because it requires fewer lines of code. There is no need to create new arrays when one will suffice.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.