2015-01-15 26 views
1
的索引

我无法使索引小于arrayList的大小,以至于我可以处理卡片。这里是我的代码:如何获得小于

import java.util.Collection; 
import java.util.Collections; 
import java.util.Random; 
import java.util.ArrayList; 

public class Deck { 
    ArrayList<Card> unusedCards = new ArrayList<Card>(); 
    ArrayList<Card> usedCards = new ArrayList<Card>(); 
    Card newCard = new Card(); 
    Random rand = new Random(); 


//Creates a Deck of 52 randomly ordered card objects (No repeats) 
public Deck() { 
    for (int i = 0; i <= 52; i++) { 
     while (unusedCards.contains(newCard)) { 
      newCard = new Card(); 
     } 
     unusedCards.add(newCard); 
    } 
} 
//shuffles the arraylist of the deck of cards, making it so the cards are dealt in a different order 
public void shuffle(){ 
    Collections.shuffle(unusedCards); 
} 

//Boolean is true when the Unsed deck has no cards left 
public boolean isEmpty(){ 
    if (unusedCards.size() == 0) return true; 
    else return false; 
} 

//returns an arraylist containing the specified number of cards 
public ArrayList<Card> Deal(int numToDeal){ 
    ArrayList<Card> DealCards = new ArrayList<Card>(); 

    for (int i = 0; i < numToDeal; i++){ 
     int index = rand.nextInt(50); 
     DealCards.add(unusedCards.get(index)); 
     usedCards.add(unusedCards.get(index)); 
     unusedCards.remove(unusedCards.get(index)); 
     } 

    return DealCards; 
} 

}

我不断收到这个错误,当我试图解决一些卡:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 48, Size: 48 
at java.util.ArrayList.rangeCheck(Unknown Source) 
at java.util.ArrayList.get(Unknown Source) 
at nnajiO.Deck.Deal(Deck.java:41) 
at nnajiO.CrazyEights.main(CrazyEights.java:26) 

感谢如果你可以提供帮助。

回答

0

尝试

int index = rand.nextInt(unusedCards.size()); 

这样你会产生一个随机数,将永远是在不使用的卡片

0

你把53卡在甲板大小的范围。

for (int i = 0; i <= 52; i++) { 

数组中索引的范围是0 .. length-1。因此,您希望在上述语句中使用<运算符,而不是运算符<=

另一个问题是类似的。

int index = rand.nextInt(50); 

在这种情况下,应更改为以下,以避免选择一个索引超出范围:您在使用静态随机范围unusedCards取出卡

int index = rand.nextInt(unusedCards.size()); 
0

在这条线要删除从列表中的元素:

unusedCards.remove(unusedCards.get(index)); 

然而,在同一个for循环,您检索从相同的列表元素少不了代码尝试访问在前一次迭代循环中删除的索引处的元素。

0

当你正在处理卡,你总是拉着从0-49的随机卡,无论卡的数量的留在unusedCards

public ArrayList<Card> Deal(int numToDeal){ 
    ArrayList<Card> DealCards = new ArrayList<Card>(); 

    for (int i = 0; i < numToDeal; i++){ 
     int index = rand.nextInt(50); // this should check the number of cards left 
     DealCards.add(unusedCards.get(index)); 
     usedCards.add(unusedCards.get(index)); 
     unusedCards.remove(unusedCards.get(index)); 
     } 

    return DealCards; 
} 

的多个卡,你试图解决,越很可能你会试图处理未使用的套牌中的一个“结束”。

让你的循环挑基础上,剩余的牌数,而不是50随机卡:

for (int i = 0; i < numToDeal; i++){ 
    int index = rand.nextInt(unusedCards.size()); 
    ... 
0

看看你的这部分代码:

for (int i = 0; i < numToDeal; i++){ 
    int index = rand.nextInt(50); 
    DealCards.add(unusedCards.get(index)); 
    usedCards.add(unusedCards.get(index)); 
    unusedCards.remove(unusedCards.get(index)); 
    } 

您正在创建一个随机指数范围在0到49之间。起初,你的数组中有53个元素,所以这是可行的。但你最后看到remove?这意味着一张卡从列表中删除,所以现在它有52个元素。

下一次你这样做的时候,还有51个,等等。最终你剩下的元素少于49个。此时,号码49不是有效的索引。所以如果随机数发生器恰好产生它,你会得到你得到的错误。

相反,您应该将index限制为unusedCards.size()

顺便说一句,如果你洗牌,真的不需要随机通过unusedCards列表。它已经是随机的顺序。只处理最后的numToDeal项目并将其删除。