我的代码出了什么问题? (链接列表java / shuffling卡)

时间:2016-02-20 14:39:53

标签: java nullpointerexception linked-list doubly-linked-list

结点类别

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);
    }
}

LinkedList类

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,而我似乎无法找出问题所在。

在shuffle方法中,我只有一个for循环,它会删除for循环所在的卡。然后将它以随机位置重新插入列表中。

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

1 个答案:

答案 0 :(得分:0)

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

解决此特定问题的一种方法是在return调用insertFirst(card)后添加insert