保持原始矢量完整,当修改它的副本 [英] keeping original Vector intact when modifying it's copy
问题描述
我要复制包含以下结构的矢量
,对我来说重要的是保留原来的 Vector
完整当我修改复制的一个:
I want to Copy a Vector
containing the following struct and the importance for me is to keep the original Vector
intact when i modify the copied one :
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);
$ b $ p <
无论我修改目的地!原来的一个修改太!!!!!!
任何想法请!它驱使我疯了!我不明白为什么!
Unfortunately all of these methods i used gives me always the same problem !! Whatever i modify the destination !!! the original one is modified too !!!!!! any idea please !! it's driving me crazy ! i don't understand why !! now am stuck since 3 days
Vector copy=new Vector((Vector) allPopulation.get(0));
for(int j=0;j<100;j++){
System.out.println("--------------------iteration num :"+(j+1)+"----------------------");
System.out.println("sol 1 ------->");
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();}
}
的变异函数如下:
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<son.size();i++){
remove = (Math.random() > 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;
}
推荐答案
从我的一个朋友,我最近发现,如果我们使用向量
包含一些数据结构
code> Class 元素,我上面提到的所有种类的副本总是导致浅拷贝。
所以解决方案是创建复制构造函数或函数以生成 DEEP复制,我们现在提供的函数是:
Finally i ve got the solution from one of my friends, lately, i discovered that if we work with a Vector
containing some Data Struct
or a Class
elements, all the kinds of copy that i mentioned above leads always to a shallow copy.
So the solution was to create a copy constructor or function to make a DEEP Copy, the proposed function we have got now is :
public Vector copi(Vector copy){
Vector callp=new Vector();
for(int i=0;i<copy.size();i++){
copy.removeAll(Collections.singleton(null));
Capaciter_n_objet cno1=null,cno=(Capaciter_n_objet) copy.get(i);
cno1=new Capaciter_n_objet();
cno1.capaiter_rest=cno.capaiter_rest;
cno1.n_objet= (Vector) cno.n_objet.clone();
callp.add(cno1);
}
return callp;
}
和所有您的建议和想法:)
And thanx for all your suggestion and ideas :)
这篇关于保持原始矢量完整,当修改它的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!