I'm trying to randomly pick 52 "card" objects from an arraylist and put them into a 2D array.
So I wrote the following code:
private Card[][] getRandomCardsArray() {
ArrayList<Card> pack = createThePack();
Card[][] cards = new Card[NUMBER_OF_ROWS][NUMBER_OF_COLS];
for(int i = 0; i < cards.length; i++) {
for(int j = 0; j < cards[i].length; j++) {
int rand = (int)(Math.random() * 51);
cards[i][j] = pack.get(rand);
pack.remove(rand);
setupCardPosition(cards[i][j],i,j); //this aids gui
}
}
return cards;
}
But of course i got a Null pointer exception in the arraylist since I'm taking out elements and I'm re-picking the same cards. I'm not sure how to fix this since I'm very new to arraylists. I have googled around but haven't found anything that works yet.
I don't get it since I'm using this:
pack.remove(rand);
doesn't that remove the index entry from the arraylist and everything?
Any help would be greatly appreciated
This post has been edited by jp612: 16 August 2011 - 12:25 AM

New Topic/Question
Reply




MultiQuote





|