我想複製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<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;
}
1.不要使用[原始類型](http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html)。 2. [不要使用'Vector'](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) - 使用類似'ArrayList'的東西。 – arshajii
現在我正在修復我的項目程序中的最後一點,我從一個月開始它..我開始與矢量..不,我不能移回來,改變處理ArrayList除了我不知道如何使用它們.. –
您應該發佈修改元素的代碼,以演示實際問題 - 因爲不清楚「保持原始完整」的意思以及您在副本上做什麼「修改」 –