0

For those who had read my first question, I think I found where the problem is. The problem is in the mutate method and especially in this instruction:

Chromosom ch=new Chromosom(); // Chromosom is a class who use hash table  
Chromosom k= new Chromosom(); // Class chromosom extends  hashmap <integer,parc>
k.initialise(); 
for(int i=0;i<l.size();i++) ch.put(i,k.get(i)); // in this instruction i think

And this is the constructor of Chromosom:

public Chromosom(){   // construct a blank chromosom
    super();
    this.identifiant = 0;
    NbEquipeExterne = 0;
    NbEquipeInterne = 0;
    CoutMinimal = 0;
    CoutMensuel = 0;
}

When I change the values of ch, the values of k change too?

How can I pass the k's values to ch by copy and not by reference?

Community
  • 1
  • 1
Sarah
  • 11
  • 2
  • You can clone the object you want to pass by copy at the beginning of the function. – vdolez Aug 12 '14 at 07:42
  • 3
    Show us the constructor of `Chromosom()`. You're probably returning the same reference. – Maroun Aug 12 '14 at 07:42
  • You can clone k.get(i) before ch.put() in case there is a possibility of updating same objects. – TJ- Aug 12 '14 at 07:43
  • You should also tell us about the class you want to copy (parc?). Probably it is better to design it immutable so that it doesn't have to be copied. – isnot2bad Aug 12 '14 at 07:45

2 Answers2

0

First of all Java is always pass-by-value. or rather pass-by-copy-of-the-variable-value not pass by reference. You might be changing objects of Chromosom with k in the constructor.

Community
  • 1
  • 1
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • I do what you ask me to do. In the first time, k have the same value as ch after changine. Now , in using yhe cinstructor, k change but not have the same value as ch. – Sarah Aug 12 '14 at 08:23
0

No danger, both ch and k are separate objects.

Furthermore you can do

ch.putAll(k);

which is a nice short-cut to remember.

Caveat

  1. If the values of the maps are stateful objects (having mutable fields to contain some changing info), then changing that field would change the field of the simgle value object in both tables.

    BitSet bits = k.get(i);
    bits.set(3);
    // Now ch.get(i) being the same object, also has bit 3 set.
    
  2. If a class has a static field, then that field is once per class, a single instance. So you could have in say Chromosome:

    static String copyright;
    
    Chromosome.copyright = "All rights reserved"; // Clearest style
    k.copyright = "All rights given to the community";
    ch.copyright = "MIT license";
    

    And you would have dealt with the same single variable, with vaklue "MIT license".

About Java

A nice design decision was made in java:

  • If you pass a variable to a function/method, the object/value will be passed. It may never happen that the variable itself gets an other object/value. (Not the address of the variable is passed.) Java has pass-by-value, no pass-by-reference.
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I use putAll, I do: ch.putAll(k) but i still have the same problem they have always the same value after changing. However, when i use a constructor to copy the value of k. the value of K change but diferent of value of ch. I don't want change the value of k, i wanted change just the value of ch. – Sarah Aug 12 '14 at 08:33
  • Maybe do a regular expression search in files for `ch\s*=` and `k\s*=` (assigments). That would be only remaining explanation. `System.out.println(ch);` resp. with `k` would show whether they really are the same instance. Maybe there is another misconception. Would it be possible that you explain where it shows as same instance? Maybe it is a mixup, copied code without changing k to ch. Or so. – Joop Eggen Aug 12 '14 at 09:33