java - 修改其副本时保持原始 Vector 完整

标签 java netbeans data-structures vector copy

我想复制一个包含以下结构的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;
 }                             

最佳答案

需要进行深度克隆,一个简单的方法就是将载体序列化并再次脱盐。您将得到预期的结果。 注意: vector 中的对象应该是可序列化的

(或)

创建一个新 vector ,然后迭代现有 vector 并将 vector 中的每个对象添加到新 vector 中

关于java - 修改其副本时保持原始 Vector 完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18731950/

相关文章:

java - 在 NetBeans 8.0.1 中构建后自动运行 junit 测试

objective-c - 实现 -hash/-isEqual :/-isEqualTo. ..:用于 Objective-C 集合

JAVA - 使用循环初始化锯齿状 3D 数组?

java - 如何保留从文件中读取的字符串的正确偏移量

java - 无法访问 eclipse 中的文件给出 : FileNotFoundException

java - Springboot同步方法

java - 分析 Gradle 项目

java - 使用状态机解析 wiki 标记

java - 在 jbuttons 上以表格格式设置数据

c++ - 在 C++ 中具有唯一条目的队列