在修改它的副本时保持原始Vector完好无损

我想复制一个包含以下结构的Vector ,对我来说重要的是在修改复制的一个时保持原始Vector完好无损:

 public class objet_poid_n { public int Num; public double Poid; } 

假设我们有:

 Vector original = new Vector(); original = filled((Vector) original); // function fills original with many objet_poid_n elements Vector destination = new Vector(); 

我试过了 :

 destination = original ; 

 destination = original.clone(); 

 destination.setSize(original.size()); Collections.copy(destination,original); 

 destination.addAll((Vector) original); 

 destination.copyInto((Vector) original); 

不幸的是,我使用的所有这些方法都给我带来了同样的问题!! 无论我修改目的地!!! 原来的一个也被修改!!!!!! 任何想法,请!! 这让我疯狂 ! 我不明白为什么!! 现在我被困3天了

  Vector copy=new Vector((Vector) allPopulation.get(0)); for(int j=0;j"); affiche_resultat((Vector) allPopulation.get(0)); \\ affiche_result to print the containing of the vector just for test System.out.println("Copy -------->"); affiche_resultat(copy); Vector muted = new Vector(); muted = mutation(copy); System.out.println("Mutated ----------->"); affiche_resultat(muted); System.out.println();} } 

突变function如下..:

 private Vector mutation(Vector son1){ Vector topac = new Vector(); // à remplir par les objet qu'on va supprimer // dégager les null Vector son = new Vector(son1); son.removeAll(Collections.singleton(null)); int j=0,i=0; boolean remove = (Math.random() > 0.3) ; // probabilité d'avoir true = 0.7 for(i=0;i 0.3); System.out.println(" remove ="+remove); if((remove == true) && (son.get(i) != null)){ Capaciter_n_objet bin =(Capaciter_n_objet) son.get(i); Vector objetlist=bin.n_objet; for(j=0;j<objetlist.size();j++) { int del =(int) Integer.parseInt(""+objetlist.get(j)); topac.add(new Integer(del)); } topac.removeAll(Collections.singleton(null)); //son.removeElementAt(i); son.setElementAt(null, i); } } son.removeAll(Collections.singleton(null)); topac.removeAll(Collections.singleton(null)); Collection noDup = new LinkedHashSet(topac); //remove duplications topac.clear(); topac.addAll(noDup); correctionmutation(son,topac); return son; } 

你需要做深度克隆,一种简单的方法是序列化载体并再次对其进行脱盐。 您将获得预期的结果。 注意:向量中的对象应该是可序列化的

(要么)

创建一个新的向量,然后迭代现有的向量并对向量中的每个对象进行colne并将其添加到新向量中

最后我从我的一个朋友那里得到了解决方案,最近,我发现如果我们使用包含一些Data StructClass元素的Vector ,我上面提到的所有类型的副本总是导致浅拷贝
因此解决方案是创建一个复制构造函数或函数来进行DEEP复制 ,我们现在提出的函数是:

  public Vector copi(Vector copy){ Vector callp=new Vector(); for(int i=0;i 

并且thanx为您的所有建议和想法:)