thread“main”java.lang.NullPointerException,在一个数组中实现三个堆栈

时间:2016-05-25 02:34:25

标签: java algorithm stack

此代码的目的是在单个数组中实现三个堆栈。我使用链接节点来实现堆栈。元素直接逐个推入数组,每个堆栈中的元素由前一个指针连接。指针是与存储项目的数组中的索引相对应的int值。 nextAvaIndex方法返回可以存储新推送项的下一个可用索引。因为在执行pop方法后,在数组的开头会释放空格。如果indexused < arr.length它将继续向前存储新项目,而如果indexused到达数组的末尾,该方法将搜索数组开头是否有空闲空间。 但是当我运行它时,它会抛出NullPointerException,我知道这个错误的含义,但我无法解决它。感谢您的意见!代码是否正确?还有一个从int类型数组中删除项的问题。我让arr[i].data = 0删除该项,并使用语句arr[i].data == 0检查一个空格是否为空。但是,如果一个空间存储0怎么办?谢谢你的建议!


public class FlexiblemultiStack {

private int[] toppoint = {-1, -1, -1};// assume number of stack ==3;
private int indexused = 0;
private stackNode[] arr;


public FlexiblemultiStack(int sizeEach, int stackNO) {

    arr = new stackNode[sizeEach * stackNO]; //

}

public boolean isEmpty(int stackNum) {
    return toppoint[stackNum] == 0;
}

public void push(int item, int stackNum) {
    int lastIndex = toppoint[stackNum];
    int nextIndex = nextAvaIndex();
    if (nextIndex == -1) {       // if nextIndex = -1, there is no more space!
        System.out.println("There is no more space!");
    } else {
        toppoint[stackNum] = nextIndex;
        arr[toppoint[stackNum]] = new stackNode(item, lastIndex);
        indexused++;
    }
}

public int pop(int stackNum) {
    if (toppoint[stackNum] == -1) {
        return 0;
    } else {
        int value = arr[toppoint[stackNum]].data;
        int lastIndex = toppoint[stackNum];
        toppoint[stackNum] = arr[toppoint[stackNum]].previous;
        arr[lastIndex] = null;
        indexused--; 
        return value;
    }
}

public int peek(int stackNum) {
    return arr[toppoint[stackNum]].data;
}

public int nextAvaIndex() {
    int index = -1;
    if (indexused == arr.length || arr[indexused].data != 0) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].data == 0) {   // error
                index = i;
                break;
            }
        }
        return index;

    } else {
        return indexused;
    }
}

public void print(int stackNum) {
    while (toppoint[stackNum] != -1) {
        System.out.print(arr[toppoint[stackNum]].data + "<--");
        toppoint[stackNum] = arr[toppoint[stackNum]].previous;
    }
}
public void printarr(){
    for(int i = 0; i< arr.length;i++){
        System.out.print(arr[i]);
    }
}
public class stackNode {  // Exception in thread "main" java.lang.NullPointerException

List item

private int previous; private int data; public stackNode(int StackSize) { this.previous = -1; } public stackNode(int value, int prev) { data = value; previous = prev; } }
private int[] toppoint = {-1, -1, -1};// assume number of stack ==3; private int indexused = 0; private stackNode[] arr; public FlexiblemultiStack(int sizeEach, int stackNO) { arr = new stackNode[sizeEach * stackNO]; // } public boolean isEmpty(int stackNum) { return toppoint[stackNum] == 0; } public void push(int item, int stackNum) { int lastIndex = toppoint[stackNum]; int nextIndex = nextAvaIndex(); if (nextIndex == -1) { // if nextIndex = -1, there is no more space! System.out.println("There is no more space!"); } else { toppoint[stackNum] = nextIndex; arr[toppoint[stackNum]] = new stackNode(item, lastIndex); indexused++; } } public int pop(int stackNum) { if (toppoint[stackNum] == -1) { return 0; } else { int value = arr[toppoint[stackNum]].data; int lastIndex = toppoint[stackNum]; toppoint[stackNum] = arr[toppoint[stackNum]].previous; arr[lastIndex] = null; indexused--; return value; } } public int peek(int stackNum) { return arr[toppoint[stackNum]].data; } public int nextAvaIndex() { int index = -1; if (indexused == arr.length || arr[indexused].data != 0) { for (int i = 0; i < arr.length; i++) { if (arr[i].data == 0) { // error index = i; break; } } return index; } else { return indexused; } } public void print(int stackNum) { while (toppoint[stackNum] != -1) { System.out.print(arr[toppoint[stackNum]].data + "<--"); toppoint[stackNum] = arr[toppoint[stackNum]].previous; } } public void printarr(){ for(int i = 0; i< arr.length;i++){ System.out.print(arr[i]); } } public class stackNode { // Exception in thread "main" java.lang.NullPointerException

private int previous; private int data; public stackNode(int StackSize) { this.previous = -1; } public stackNode(int value, int prev) { data = value; previous = prev; } }

线程“main”java.lang.NullPointerException中的异常     at stackandqueue.FlexiblemultiStack $ stackNode.access $ 000(FlexiblemultiStack.java:86)     at stackandqueue.FlexiblemultiStack.nextAvaIndex(FlexiblemultiStack.java:61)     at stackandqueue.FlexiblemultiStack.push(FlexiblemultiStack.java:32)     at stackandqueue.StackandQueue.main(StackandQueue.java:71) /Users/xchen011/Library/Caches/NetBeans/8.1/executor-snippets/run.xml:53:Java返回:1 BUILD FAILED(总时间:0秒)

1 个答案:

答案 0 :(得分:0)

pop()方法中,您似乎通过将数组索引设置为null(arr[lastIndex] = null)来表示开放索引。在nextAvaIndex()中,您可以通过检查arr[i].data来检查索引是否可用。如果arr[i]null设置为pop(),您将获得NullPointerException。要使可用定义与可用性检查一致,请尝试将arr[indexused].data != 0替换为arr[indexused] != null,将if(arr[i].data == 0)替换为if(arr[i] == null)方法中的nextAvaIndex()

public int nextAvaIndex() {
    int index = -1;
    if (indexused == arr.length || arr[indexused] != null) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {   // error
                index = i;
                break;
            }
        }
        return index;

    } else {
        return indexused;
    }

}