0

Here is my code and it works perfectly fine.

import java.util.Random;
class apples
{
    public static void main(String args[])
    {
         Random rand = new Random();
         int frequency[] = new int[7];

         for(int roll = 1;roll < 1000;roll++){
             ++frequency[1+rand.nextInt(6)];
         }

         System.out.println("Face\tFrequency");

         for(int face = 1;face < frequency.length;face++){
             System.out.println(face + "\t" + frequency[face]);
         }
    }
}

I do not understand this line of code

++frequency[1+rand.nextInt(6)];

When I removed the "++" operator, it couldn't be compiled. I know that it will add 1 to the randon numbers generated from 0 to 5 but why there is a "++" in front of frequency ? Why is it neccessary to put the "++" operator there ?

1
  • Likely it was just a warning that the statement does nothing; not a compiler error. If you're able to run the program, then it was not an error, but a warning. Commented Dec 9, 2013 at 23:34

4 Answers 4

3

The ++ operator is incrementing the frequency at the specified index. In this case, it's the same as saying:

for(int roll = 1;roll < 1000;roll++){
  int index = 1+rand.nextInt(6);
  frequency[index] = frequency[index] + 1;
}

Removing the ++ operator, you're left with:

for(int roll = 1;roll < 1000;roll++){
  frequency[1+rand.nextInt(6)];
}

The line frequency[1+rand.nextInt(6)] makes no sense; it is not an operation, it does not do anything.


EDIT:

Perhaps a better illustration: let x be the value looked up in the frequency array. Then the original is equivalent to:

for(int roll = 1;roll < 1000;roll++){
  int x = frequency[1+rand.nextInt(6)];
  ++x; // Equivalent to "x = x + 1"
}

Whereas if you remove the increment operator, your resulting loop would be:

for(int roll = 1;roll < 1000;roll++){
  int x = frequency[1+rand.nextInt(6)];
  x; // ...what?
}
Sign up to request clarification or add additional context in comments.

Comments

0

The statement without the ++ looks at the contents of the array, but does not do anything with the value. Adding the ++ operator tells it to increment the value found there.

You probably are thinking that frequency[1+rand.nextInt(6)] is adding a value to the array. It is not. It is looking up a location in memory from a random location.

Comments

0

++ is a shorthand for

int i = 1+rand.nextInt(6);
frequency[i] = frequency[i] + 1;

Removing it causes no increment expression is executed.

Comments

0

that line is equivalent to this line of code

int index = 1+rand.nextInt(6);
frequency[index] = frequency[index]+1 ;

so if you removed '++' you make the statement incomplete like the following:

 int x= 5;
 x;

so it gives you error

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.