Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';我的代码怎么了?(链表java/洗牌卡)_Java_Nullpointerexception_Linked List_Doubly Linked List - Fatal编程技术网

什么';我的代码怎么了?(链表java/洗牌卡)

什么';我的代码怎么了?(链表java/洗牌卡),java,nullpointerexception,linked-list,doubly-linked-list,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()

节点类

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);
    }
}
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!");
}
}
LinkedList类

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);
    }
}
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!");
}
}
大家好。我正在制作一个包含许多卡片的双链接列表。 一切似乎都很好(当我单独测试每个方法时,它似乎都很有效)。 但是当我运行shuffle方法时,偶尔会出现
NullPointerException
,我似乎无法找到问题所在

在shuffle方法中,我只有一个for循环,它移除了for循环所在的牌。然后,它将它插入到列表中的任意位置


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

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


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

谢谢,伙计,解决了此问题。你是我的英雄:D。我可以问一下你找到问题的方法吗?我在循环中运行了你的代码来复制问题。shuffle中的代码显示了它在大小上循环了多少次,循环次数达到53次以上,因此很明显大小增加了太多次。我看了看它可以改变的地方,试图找到一条它可以改变不止一次的路径。