【问题标题】:Java ArrayList, LinkedList & Stack problemJava ArrayList、LinkedList 和 Stack 问题
【发布时间】:2011-03-02 20:08:53
【问题描述】:

我有以下代码:

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

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

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

米克

【问题讨论】:

  • 堆栈跟踪说明了什么?在哪一行抛出异常?
  • @Boris 嗨,鲍里斯,我一发布信息就更新了描述,因为我忘记了包含它 - 现在描述正确。谢谢。
  • 太棒了。虽然现在已经发布了答案:-)

标签: java stack arraylist linked-list


【解决方案1】:

在第一种情况下,您传入 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) 更好地反映命名。编译器会强制你做一些清理工作,正确地做会消除问题,或者让问题所在的地方更加明显。

【讨论】:

  • 我明白了...是导致错误的“null”吗?我没有列出其他人,因为我认为它不起作用。所以在每种情况下,我都需要列出“arrayList”、“linkedList”和“stack”,对吗?例如,与“null”、“linkedList”和“stack”相反?这有意义吗?如果是,对吗?
  • 是的......有点。它会解决null 的问题,但我认为它不会解决您的代码的结构性问题——它对linkedListstack 没有任何作用。
【解决方案2】:

在某一时刻,您正在调用:

printCollection(null, linkedList, null);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2014-10-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    相关资源
    最近更新 更多