Tuesday, 15 March 2011

java - IndexOutOfBoundsException while trying to shuffle deck -


I am trying to manipulate the deck of cards.

  Public Zero Alteration () {Int rand1; Int rand2; Random randomGenerator = new random (); (Int i = 0; i & lt; deck.size (); i ++) {// between a random pointer 0 and the size of the deck - 1 rand1 = randomGenerator.nextInt (10); Rand2 = randomGenerator.nextInt (10); // swap rand1 and rand2 card temp = deck.get (rand 1); Deck .set (rand 1, deck .get (rand 2)); Deck .Set (Rand 2, Temp); }}   

Then it says:

  java.lang.IndexOfOfBoundsException; Index: 8; > 

This line

  rand1 = randomGenerator.nextInt (10);   

should be

  rand1 = randomGenerator.nextInt (deck size ());   

and similarly for rand2

No comments:

Post a Comment