为什么我得到不同的行为有:的Java中的addAll(集合)对新的ArrayList(集合)
Collection col2 = new ArrayList(col);
Collection col2 = new ArrayList();
col2.addAll(col)
我的工作与观众,和代码是复杂的,我试图解释问题的“根源” 。另一个有趣的事实是下一个...
//IF i use this code i have the correct behavior in my app:
public void updateCollection(Collection<Object> col) {
this.objectCollection.clear();
this.objectCollection.addAll(col);
}
//IF i use this code i have unexpected behavior in my app:
public void updateCollection(Collection<Object> col) {
this.objectCollection=new ArrayList(col);
}
请澄清一下,哪种确切的行为令您感到困惑? – 2010-11-21 15:36:44
我的代码使用a)或b)有不同的行为。在我看来,这两次行动都会导致相同的结果,但显然他们没有。有些不同。 – marcolopes 2010-11-21 15:55:02
你还是太模糊。怎么了?不会发生什么?请在您的环境中发布[SSCCE](http://sscce.org)以及(未)预期结果。 – BalusC 2010-11-21 15:56:05