# Thread: data get duplicated on a Vector

1. Member
Join Date
Apr 2011
Posts
8
Rep Power
0

## 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

population.addElement(x1);
population.addElement(x2);
population.addElement(x3);

}

Thank you

2. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
11
// initialize x1,x2,x3 from some function

Thats the important part. Show us this method!

3. 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

4. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
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

5. 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.

6. Member
Join Date
Apr 2011
Posts
8
Rep Power
0

## 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

7. 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

8. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
yes, that's the purpose.
would you please explain what is wrong.

9. 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.

10. 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:

Java Code:
```int[] GenerateChromosome()
{

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

kind regards,

Jos

11. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
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 ?

12. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
I just notice your post sorry, I'll do what u said

13. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
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

14. Member
Join Date
Apr 2011
Posts
8
Rep Power
0
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 :-)

15. 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•