Java ArrayList,LinkedList&堆栈问题

时间:2011-03-02 20:08:53

标签: java stack arraylist linked-list

我有以下代码:

public static void printCollection(ArrayList<Data> al, LinkedList<Data> ll, Stack<Data> stack){

    for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
        Data x = (Data)iter.next();
        x.print();
    }

    System.out.println();

}

public static void main(String[] args){

    Data x = new Data("Fred", 41);
    x.print();

    //ArrayList
    ArrayList<Data> arrayList = new ArrayList<Data>();

    //LinkedList
    LinkedList<Data> linkedList = new LinkedList<Data>();

    //Stack
    Stack<Data> stack = new Stack<Data>();

    //'people' variables
    Data person1 = new Data("Fred", 21);
    Data person2 = new Data("Jane", 21);
    Data person3 = new Data("Zoe", 23);
    Data person4 = new Data("Harry", 78);

    //ArrayList
    arrayList.add(person1);
    arrayList.add(person2);
    arrayList.add(person3);
    arrayList.add(2, person4);

    printCollection(arrayList, null, null);

    //LinkedList
    linkedList.add(person1);
    linkedList.add(person2);
    linkedList.add(person3);
    linkedList.add(2, person4);

    printCollection(null, linkedList, null);

    //Stack
    stack.push(person1);
    stack.push(person2);
    stack.push(person3);
    stack.push(person4);

    while(stack.isEmpty() == false)
    {
        stack.pop().print();
    }
    System.out.println(stack.size());

}

...产生NullPointerException错误。但是,如果我删除一些代码行使它看起来像这样:

public static void printCollection(ArrayList<Data> al, LinkedList<Data> ll, Stack<Data> stack){

    for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
        Data x = (Data)iter.next();
        x.print();
    }

    System.out.println();

}

public static void main(String[] args){

    Data x = new Data("Fred", 41);
    x.print();

    //ArrayList
    ArrayList<Data> arrayList = new ArrayList<Data>();

    //LinkedList
    LinkedList<Data> linkedList = new LinkedList<Data>();

    //Stack
    Stack<Data> stack = new Stack<Data>();

    //'people' variables
    Data person1 = new Data("Fred", 21);
    Data person2 = new Data("Jane", 21);
    Data person3 = new Data("Zoe", 23);
    Data person4 = new Data("Harry", 78);

    //ArrayList
    arrayList.add(person1);
    arrayList.add(person2);
    arrayList.add(person3);
    arrayList.add(2, person4);

    printCollection(arrayList, null, null);

}

}

...然后它运行得很好。我已多次检查并且无法检测到错误(没有双关语意图(NullPointerException))。

错误一直出现在以下行:

for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
//remaining code omitted for illustration purposes

我不知道它可能是什么,需要一些新鲜的眼睛来帮助我看看。

感谢您抽出宝贵时间阅读本文。

米克

2 个答案:

答案 0 :(得分:3)

在第一种情况下,您正在传递null以获取正在使用的唯一集合:al,当您要求它为迭代器时,它会抛出一个NPE。在第二种情况下不会发生这种情况。

另一个注意事项:您无需投出iter.next()来电。

要解决此问题,您可能需要将printCollection的签名更改为:

public static void printCollection(Collection<Data> c){
 for(Iterator<Data> iter = c.iterator(); iter.hasNext();) iter.next().print();
 System.out.println();    
}

因为那是(1)该方法发生了什么,(2)更好地反映了命名。编译器将强制您进行一些清理,并且正确地执行此操作将消除问题或使问题显得更加明显。

答案 1 :(得分:0)

有一次你在打电话:

printCollection(null, linkedList, null);

并查看您方法的声明。它试图从第一个参数获取迭代器。这导致你的NPE。