我的代码有什么问题? (链表 java/shuffling 卡片)

What's wrong with my code? (Linked List java/shuffling cards)

节点类

public class NodeDouble{
    private int card;
    NodeDouble next;
    NodeDouble prev;

    public NodeDouble(int card){
        this.card = card;
    }

    public int getCard(){
        return this.card;
    }

    public NodeDouble getNext(){
        return this.next;
    }

    public NodeDouble getPrev(){
        return this.prev;
    }

    public void displayNode(){
        System.out.println("Card: "+card);
    }
}

链表class

import java.util.Random;

public class CardListDouble{

private NodeDouble head;
private NodeDouble tail;
private int size;

//constructors
public CardListDouble(){
    head = null;
    tail = null;
    size = 0;
}
public CardListDouble(int numberOfCards){
    for(int i = numberOfCards; i>=1; i--){
        this.insertFirst(i);
    }
    size = numberOfCards;
}
//methods
public void insert(int card, int index){
    if(index < 1 || index > size+1){throw new IllegalArgumentException("index is smaller than 1!, or larger than size!");}
    else if(index == 1){//add at beginning
        insertFirst(card);
    }
    else if(index == size+1){//add to the end
        NodeDouble temp = new NodeDouble(card);

        tail.next = temp;
        temp.prev = tail;
        tail = temp;
    }else{//add to the 'middle' somewhere
        NodeDouble temp = new NodeDouble(card);
        NodeDouble indexmin1 = getNode(index-1);
        NodeDouble indexcurrent = getNode(index);

        indexmin1.next = temp;
        temp.next = indexcurrent;
        indexcurrent.prev = temp;
        temp.prev = indexmin1;
    }size++;
}

public void insertFirst(int card){
    NodeDouble temp = new NodeDouble(card);

    if(isEmpty()){
        head = temp;
        tail = temp;
    }else{
        temp.next = head;
        head.prev = temp;
        head = temp;
    }size++;
}

public int removeCard(int card){
    if(card > size){throw new IllegalArgumentException("card not here!");}
    if(head == null){System.out.println("cannot delete from empty list");}
    else{
        NodeDouble current = head;
        NodeDouble oneBehind = null;
        boolean found = false;
        int fua = 0;
        //traverse list to find which node/card to delete
        while(found == false && fua < size){
            fua++;
            if(current.getCard() == card){
                found = true;
                break;
            }else{
                current = current.next;
            }

        }
        //did not find node
        if(current == null){System.out.println("Card not in list!");}
        //did find node
        else{
            int tempCard = current.getCard();
            if(head == current){//case1; card is at head
                    head = head.next;
                    head.prev = null;
                }
                else if(current == tail){//case2; card is tail
                    tail = tail.prev;
                    tail.next = null;
                }
                else{//card is in the 'middle' somewhere
                current.prev.next = current.next;
                current.next.prev = current.prev;
                current.next = null;
                current.prev = null;
                }size--;return tempCard;
            }
        }return 0;//did not find card so return 0;
    }




public NodeDouble getNode(int index){
    if(index <=0){throw new IllegalArgumentException("index < 1 yo! (getnodemethod)");}
    else if(index > size){throw new IllegalArgumentException("index is bigger than size!");}
    else if(index < (size / 2)){//traverse from right
        NodeDouble current = head;
        for (int i = 1; i < index; i++) {
            current = current.next;
        }return current;
    }else if(index >= (size/2)){//traverse from left
        NodeDouble current = tail;
        for (int i = size; i > index; i--) {
            current = current.prev;
        }return current;
    }else{
        System.out.println("list is empty or index is smaller than 0!");
        return null;
    }
}

public void displayList(){
    if(isEmpty()){
        System.out.println("Empty List!");
    }else{
        NodeDouble current = head;

        while(current != null){
            current.displayNode();
            current = current.next;
        }
    }System.out.println("size of deck:" + size);
}
public boolean isEmpty(){
    return size == 0;
}

public void shuffle(){
    Random rng = new Random();
    for(int i = 1; i<size; i++){
        int temp = rng.nextInt(size) + 1;
        System.out.println("i:" + i + " temp: " + temp);
        removeCard(i);
        insert(i,temp);
    }
    System.out.println("shuffling done!");
}
}

测试/主要方法

   public class Test{

    public static void main(String[] args){
        CardListDouble list = new CardListDouble(52);

        long startTime = System.currentTimeMillis();
        list.shuffle();
        list.displayList();
        long estimatedTime = System.currentTimeMillis() - startTime;
        System.out.println("Time taken: " + estimatedTime);
    }

大家好。我正在制作一个包含许多卡片的双向链表。 一切似乎都工作正常(当我单独测试每种方法时,它似乎工作)。 但是当我运行 shuffle 方法时,偶尔会得到一个NullPointerException,我似乎无法找出问题所在。

在洗牌方法中,我只有一个 for 循环,它删除 for 循环所在的卡片。然后它将其插入列表中的随机位置。

我是初学者,希望您能理解我的代码。我添加了一些评论。

在您的 insert 方法中,您的一个案例调用 insertFirst 并继续。发生这种情况时,insertFirst 会增加列表的大小,然后 insert 会再次增加列表的大小。现在列表的大小太大了,当它结束时你会在 remove 中得到一个 NullPointerException

解决此特定问题的一种方法是在 insert.

中调用 insertFirst(card) 之后添加 return