i have a problem with my script
i have a method that return a card, another return the name, another the value and another the suit
public Card pickRandomCard() {
while (rand < 6) {
System.out.println(deckOfCards.get(rand));
int cardValuePlayer1 = (deckOfCards.get(rand).toInt());
currentTotal1 = cardValuePlayer1;
String name1 = (deckOfCards.get(rand).toString());
nameF = name1;
String suit_1 = (deckOfCards.get(rand).suit());
suit1 = suit_1;
return deckOfCards.get(rand++);
}
return null;
}
public int totalValuePlayer1() {
return currentTotal1;
}
public String name1() {
return nameF;
}
public String suit_1() {
return suit1;
}
but now, i want to create an arraylist of cards, like a hand of cards, so my ideia is create an arraylist with 5 cards, but if i did something like
List<Card> hand = new ArrayList<Card>();
Iterator<Card> iter = hand.iterator();
while (iter.hasNext()) {
hand.add(gr.pickRandomCard());
}
i receive Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 0
i have this code in another class to call each method, suit, value of card, image, but i need to create a list to compare cards, and with the code below, it is impossible to compare a card one by one
ActionListener one = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (gr1.getCounter1() < 5) {
gr1.setCounter1(gr1.getCounter1() + 1);
test1.setIcon(play1a);
pn1.setText(Integer.toString(play2a));
pn5.setText(play3a);
pn50.setText(play4a);
} else {
pn5.setText("No more cards");
}
}
};
arraybtn[1].addActionListener(one);
arraybtn[1].setPreferredSize(new Dimension(120, 20));
play1a = gr.pickRandomCard().getImage();
play2a = gr.totalValuePlayer1();
play3a = gr.name1();
play4a = gr.suit_1();
arraybtn[1].setText(play3a);//change the name of button
i think the problem is that i need to create 4 list, each for correspondent method (suit, image, value), but i don't have any idea how i can do that
thanks