问题描述
大家好,我正在用java做作业。我正在使用Collections类中提供的Collections.copy方法。以下是我的代码片段。
Hello Everyone, I am doing an assignment in java. I am making use of Collections.copy method available in Collections class. Following is a snippet of my code.
public Queue(List<E> queue1) {
queue = new ArrayList<E>(queue1.size());
System.out.println("const" + queue1.size());
// Collections.copy(this.queue, queue1);
this.queue = queue1;
}
问题是当我使用Collections.copy方法时它给我一个NullPointerException,我不知道为什么?当我使用'='直接分配时,它工作正常。以下是代码片段,我在这里调用这个构造函数。
The problem is when I'm using Collections.copy method its throwing me a NullPointerException, I don't know why? When I'm assigning directly using '=', it's working fine. Following is the snippet of code, where I am invoking this construcor.
List<Integer> e = new ArrayList<Integer>(3);
e.add(1);
e.add(2);
e.add(3);
System.out.println(e.size());
Queue<Integer> p = new Queue<Integer>(e);
任何人都知道如何解决它,请帮助我。我需要在这里使用collections.copy。
谢谢和问候,
Vinay Kumar Tiwary
Anyone knows how to solve it, please help me out. I need to use collections.copy here.
Thanks and regards,
Vinay Kumar Tiwary
推荐答案
public Queue(List<E> queue1) {
queue = new ArrayList<E>(queue1);
Collections.copy(this.queue, queue1);
}
这篇关于java中的Collections.copy方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!