# data get duplicated on a Vector

• 04-04-2011, 05:13 PM
khlitoshi
data get duplicated on a Vector
Hey,

I really need help

while using Vector and adding elements, I get this problem:
I add the 1st element: OK
I add the 2nd element: 2 elements in the vector, but the first element gets the value of the second element
I add the 3rd element: the vector contains 3 elements, all have the value of the 3rd element
..and so on

here is the source code (written only the essential not to make it complicated for you)

public static void main(String[] args) throws IOException, InterruptedException {
int[] x1,x2,x3;

Vector<int[]> population= new Vector<int[]>(50);

// initialize x1,x2,x3 from some function

}

Thank you
• 04-04-2011, 05:43 PM
eRaaaa
// initialize x1,x2,x3 from some function

Thats the important part. Show us this method!
• 04-04-2011, 05:47 PM
KevinWorkman
• 04-04-2011, 05:59 PM
JosAH
Quote:

Originally Posted by khlitoshi
while using Vector and adding elements, I get this problem:
I add the 1st element: OK
I add the 2nd element: 2 elements in the vector, but the first element gets the value of the second element
I add the 3rd element: the vector contains 3 elements, all have the value of the 3rd element
..and so on

That's a symptom that you store one single object in your Vector multiple times; when you change the content of one of them, 'all' (mind the quotes) of them in your Vector change.

kind regards,

Jos
• 04-05-2011, 04:40 AM
khlitoshi
If I post the initialization part, it will get complicated for you and no one will ever read it
I made sure with debugging that x1,x2,x2 are initialized correctly and the problem occurs at the "addElement" method level.

would you please try to run it, I'm really blocked
• 04-05-2011, 04:46 AM
Junky
How are we supposed to run it if you do not supply all the code? No, that was not a plea for you to dump a gazillion lines of code on the forum.

As already pointed out, 99% of the time the cause of this is due to all the variables pointing at the same object.
• 04-05-2011, 06:45 AM
khlitoshi
source code
please find attached the source code.

in file "Knapsack" line "52" there's a call for the method "addElement" in which there's the problem.

Thanks
• 04-05-2011, 07:03 AM
JosAH
Quote:

Originally Posted by khlitoshi
please find attached the source code.

in file "Knapsack" line "52" there's a call for the method "addElement" in which there's the problem.

Thanks

You're adding chromosome arrays, filled by a Chromosome object, to your knapsack. That array is the same array (with different content) over and over again, just as I guessed in my first reply.

kind regards,

Jos
• 04-05-2011, 07:14 AM
khlitoshi
yes, that's the purpose.
would you please explain what is wrong.
• 04-05-2011, 07:35 AM
Junky
When you add an object to a Collection you add the reference to that object and not the object itself.

Lets pretend that a piece of paper stuck to the wall is your array. You write "dog", "cat" and "monkey". On the paper. You then get John to point at the paper. You then cross out "cat" and replace it with "pig" and get Mike to point at the piece of paper.

Now your expectations is that John is pointing to a different piece of paper than Mike is pointing at. Whereas they are both pointing at the same piece of paper with dog, pig and monkey written on it.
• 04-05-2011, 07:51 AM
JosAH
Quote:

Originally Posted by khlitoshi
yes, that's the purpose.
would you please explain what is wrong.

Add one line to the start of your GenerateChromosome method, like this:

Code:

```int[] GenerateChromosome() {                         chromosome= new int[total_items];         ...```
Now that method returns a new array each time it is called.

kind regards,

Jos
• 04-05-2011, 07:56 AM
khlitoshi
thanks for the explanation,
to fix that, I added "chromosome_buf = new int[500]" at the beginning of the loop, so that each time I get a new memory allocation for the array. but still the same problem...
how would you fix the problem if it were you ?
• 04-05-2011, 07:57 AM
khlitoshi
I just notice your post sorry, I'll do what u said
• 04-05-2011, 08:00 AM
khlitoshi
you fixed the problem with your solution, thanks so much

adding "chromosome_buf = new int[total_items]" at the loop start doesn't help
why is that?

thanks again
• 04-05-2011, 08:02 AM
khlitoshi
I'll not bother you, as you said earlier its a problem of "Linkage" I'll find it myself

Thank you very much Mr.JosAH for the solution, you saved my life :-)
• 04-05-2011, 08:44 AM
JosAH
Quote:

Originally Posted by khlitoshi
I'll not bother you, as you said earlier its a problem of "Linkage" I'll find it myself

Thank you very much Mr.JosAH for the solution, you saved my life :-)

Carefully reread my previous reply; I didn't mention a chromosome_buf anywhere, simply allocated a new chromosome array each time you generate one.

kind regards,

Jos