Java ArrayList、LinkedList和;堆栈问题

Java ArrayList、LinkedList和;堆栈问题,java,stack,arraylist,linked-list,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();

我有以下代码:

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

}
publicstaticvoidprintcollection(arraylistal、LinkedList ll、Stack){
for(迭代器iter=al.Iterator();iter.hasNext();){
数据x=(数据)iter.next();
x、 打印();
}
System.out.println();
}
公共静态void main(字符串[]args){
数据x=新数据(“Fred”,41);
x、 打印();
//ArrayList
ArrayList ArrayList=新的ArrayList();
//链接列表
LinkedList LinkedList=新建LinkedList();
//堆叠
堆栈=新堆栈();
//“人”变量
Data person1=新数据(“Fred”,21);
Data person2=新数据(“Jane”,21);
Data person3=新数据(“Zoe”,23);
Data person4=新数据(“Harry”,78);
//ArrayList
arrayList.add(person1);
arrayList.add(person2);
arrayList.add(person3);
arrayList.add(2人,4人);
printCollection(arrayList,null,null);
//链接列表
linkedList.add(person1);
linkedList.add(person2);
linkedList.add(person3);
linkedList.add(2人,4人);
printCollection(null,linkedList,null);
//堆叠
栈。推(人1);
栈。推(人2);
栈。推(人3);
栈。推(人4);
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);

}

}
publicstaticvoidprintcollection(arraylistal、LinkedList ll、Stack){
for(迭代器iter=al.Iterator();iter.hasNext();){
数据x=(数据)iter.next();
x、 打印();
}
System.out.println();
}
公共静态void main(字符串[]args){
数据x=新数据(“Fred”,41);
x、 打印();
//ArrayList
ArrayList ArrayList=新的ArrayList();
//链接列表
LinkedList LinkedList=新建LinkedList();
//堆叠
堆栈=新堆栈();
//“人”变量
Data person1=新数据(“Fred”,21);
Data person2=新数据(“Jane”,21);
Data person3=新数据(“Zoe”,23);
Data person4=新数据(“Harry”,78);
//ArrayList
arrayList.add(person1);
arrayList.add(person2);
arrayList.add(person3);
arrayList.add(2人,4人);
printCollection(arrayList,null,null);
}
}
…然后它就可以运行了。我检查了多次,无法检测到错误(没有双关语(NullPointerException))

错误一直出现在以下行中:

for(Iterator<Data> iter = al.iterator(); iter.hasNext();){
//remaining code omitted for illustration purposes
for(迭代器iter=al.Iterator();iter.hasNext();){
//为便于说明,省略了剩余代码
我不知道它可能是什么,需要一些新鲜的眼睛来帮助我,看看

感谢您抽出时间阅读此文章


Mick

在您呼叫的某个点:

printCollection(null,linkedList,null);


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

在您调用的某个点:

printCollection(null,linkedList,null);


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

在第一种情况下,您为正在使用的唯一集合传递了
null
al
,当您向NPE请求迭代器时,它当然会抛出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();    
}
publicstaticvoidprintcollection(集合c){
对于(迭代器iter=c.Iterator();iter.hasNext();)iter.next().print();
System.out.println();
}

因为这是(1)方法发生的事情和(2)更好地反映命名。编译器将强制您进行一些清理,正确地进行清理将消除问题或使问题更加明显。

在案例一中,您为正在使用的唯一集合传递
null
al
,当您询问对于迭代器,这是不可能的

另一个注意事项:您不需要强制执行
iter.next()
调用

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

public static void printCollection(Collection<Data> c){
 for(Iterator<Data> iter = c.iterator(); iter.hasNext();) iter.next().print();
 System.out.println();    
}
publicstaticvoidprintcollection(集合c){
对于(迭代器iter=c.Iterator();iter.hasNext();)iter.next().print();
System.out.println();
}

因为这是(1)方法发生的事情和(2)更好地反映命名。编译器将强制您进行一些清理,正确地进行清理将消除问题或使问题更加明显。

堆栈跟踪说明了什么?在哪一行引发异常?@Boris Hi Boris,我一发布信息就更新了描述,因为我忘了包括它-现在描述正确了。谢谢。很好。虽然现在已经发布了答案:-)堆栈跟踪告诉了什么?异常是在哪一行引发的?@Boris嗨Boris,我一发布信息就更新了描述,因为我忘了包括它-现在描述正确了。谢谢。很好。尽管answe我明白了…是“null”导致了这些错误吗?我没有列出其他的,因为我认为它不起作用。所以在每种情况下,我都需要列出“arrayList”、“linkedList”和“stack”,对吗?与“null”、“linkedList”和“stack”相反,比如说,这有意义吗?如果有,是这样吗?是的…或者当然。它会解决
null
问题,但我认为它不会解决您的代码的结构性问题-它对
linkedList
stack
没有任何作用。我明白了……是“null”导致了错误吗?我没有像我所想的那样列出其他代码