我剛剛爲我的Java2課程開始了一個項目,我已經完全停下來了。我無法得到 這個方法。特別是當賦值不允許我們使用java中的任何其他數據結構或shuffle方法時。我該如何混洗鏈接列表中的節點?
所以我有一個Deck.class,其中我已經創建了一個鏈表,其中包含52個節點,可容納52張卡片。
public class Deck {
private Node theDeck;
private int numCards;
public Deck()
{
while(numCards < 52)
{
theDeck = new Node (new Card(numCards), theDeck);
numCards++;
}
}
public void shuffleDeck()
{
int rNum;
int count = 0;
Node current = theDeck;
Card tCard;
int range = 0;
while(count != 51)
{
// Store whatever is inside the current node in a temp variable
tCard = current.getItem();
// Generate a random number between 0 -51
rNum = (int)(Math.random()* 51);
// Send current on a loop a random amount of times
for (int i=0; i < rNum; i ++)
current = current.getNext(); ******<-- (Btw this is the line I'm getting my error, i sort of know why but idk how to stop it.)
// So wherever current landed get that item stored in that node and store it in the first on
theDeck.setItem(current.getItem());
// Now make use of the temp variable at the beginning and store it where current landed
current.setItem(tCard);
// Send current back to the beginning of the deck
current = theDeck;
// I've created a counter for another loop i want to do
count++;
// Send current a "count" amount of times for a loop so that it doesn't shuffle the cards that have been already shuffled.
for(int i=0; i<count; i++)
current = current.getNext(); ****<-- Not to sure about this last loop because if i don't shuffle the cards that i've already shuffled it will not count as a legitimate shuffle? i think? ****Also this is where i sometimes get a nullpointerexception****
}
}
}
現在,我得到的各種錯誤 時,我呼籲這個方法:
它有時會洗牌只是2張卡,但有時它會洗牌3 - 5張牌,然後給我一個空指針異常。 我已經指出,它給了我這個錯誤用星號在我上面的代碼
在一個點上我得到了它的洗牌13卡,但隨後每次它這樣做,它並沒有完全打亂他們的正確途徑。一張牌總是重複。
在另一點我得到了所有52張牌通過while循環,但它又一次重複一張牌。
所以我真的需要一些輸入我做錯了什麼。在我的代碼結束時,我認爲我的邏輯完全錯誤,但我似乎無法找出解決方法。
是的,我想到了,但可悲的是,我的教授不會允許另一個數據結構來幫助我們使用這種方法。不過謝天謝地,我想出瞭如何去做。謝謝你。 – Raw415 2013-03-27 01:31:30
順便提一下,上述建議同樣適用於沒有輔助數據結構的情況......您可以將列表的末尾視爲輔助列表,並在列表開始處保留剩餘項目的數量以便隨機播放。請注意,對於定義良好的列表類,使用「移動節點」語義,使用輔助數據結構不會使用更多的內存,不會進行單個分配並使分離更清晰。 – armel 2013-03-27 08:42:13