1

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;
                       }

supposing that we have :

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

I tried :

destination = original ;

and

destination = original.clone();

and

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

and

destination.addAll((Vector) original);

and

destination.copyInto((Vector) original);

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();}


    }       

the mutation function is as follow .. :

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;
 }                             
Ghassen Bellagha
  • 249
  • 3
  • 5
  • 16
  • 3
    1. Don't use [raw types](http://docs.oracle.com/javase/tutorial/java/generics/rawTypes.html). 2. [Don't use `Vector`](http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) - use something like `ArrayList` instead. – arshajii Sep 11 '13 at 03:03
  • now am fixing the last points in my project program i started it since a month .. and i started it with Vectors .. no i can't move back and change to deal with ArrayList besides i don't know how to use them .. – Ghassen Bellagha Sep 11 '13 at 03:06
  • You should post the code that modifies the elements, to demonstrate the actual problem - because it's not clear what you mean with "keeping the original intact" and what "modifications" you're doing on the copy – Óscar López Sep 11 '13 at 03:11
  • You should be using references of type `List` so the implementation is irrelevant to all code except the lines actually instantiating the implementations. That way you can easily swap out implementations seamlessly if necessary (e.g. use a LinkedList instead if appropriate). –  Sep 11 '13 at 04:43
  • @JohnGaughan i really need a solution for having an editable copy without modifying the original or mess with it's data .. – Ghassen Bellagha Sep 11 '13 at 06:33
  • If you started the project a mere two months ago why is the code using features that were deprecated in JDK 1.5?! Don't use raw types. Don't user Vectors. And do use enhanced for each loops. – Boris the Spider Sep 11 '13 at 07:43

2 Answers2

0

you need to do deep cloning, A simple way is to serialize the vector and desalinize it again. You will get the expected result. Note: Objects in the vector should be serializable

(or)

create a new vector, Then iterate over the existing vector and colne each object in the vector and add to new it to new vector

upog
  • 4,965
  • 8
  • 42
  • 81
  • 2
    You need to reread more closely. Clone each object into the new list assuming it is cloneable. Or use serialization, which is a deep operation. But that relies on the constituent objects being serializable or cloneable. –  Sep 11 '13 at 04:44
  • `Vector copy=new Vector(); copy.setSize(((Vector) allPopulation.get(0)).size()); for(int i=0;i<((Vector) allPopulation.get(0)).size();i++){ copy.addElement((Capaciter_n_objet) (((Vector) allPopulation.get(0)).get(i))); }` i tried this and it still affect the original vector – Ghassen Bellagha Sep 11 '13 at 05:30
  • @GhassenBellagha not sure I have ever seen uglier code. In any case you are still not reading the answer, you are just moving references. – Boris the Spider Sep 11 '13 at 08:33
0

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 :)

Ghassen Bellagha
  • 249
  • 3
  • 5
  • 16