2015-01-15 69 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項目並將其刪除。