Results 1 to 16 of 16
Thread: data get duplicated on a Vector
- 04-04-2011, 05:13 PM #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
- 04-04-2011, 05:43 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
// initialize x1,x2,x3 from some function
Thats the important part. Show us this method!
- 04-04-2011, 05:47 PM #3
Crossposted: Using Vector, data get duplicated
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-04-2011, 05:59 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-05-2011, 04:40 AM #5
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
- 04-05-2011, 04:46 AM #6
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 #7
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
- 04-05-2011, 07:03 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-05-2011, 07:14 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
yes, that's the purpose.
would you please explain what is wrong.
- 04-05-2011, 07:35 AM #10
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 #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-05-2011, 07:56 AM #12
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 ?
- 04-05-2011, 07:57 AM #13
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
I just notice your post sorry, I'll do what u said
- 04-05-2011, 08:00 AM #14
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
- 04-05-2011, 08:02 AM #15
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 :-)
- 04-05-2011, 08:44 AM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
ArrayList duplicated/replaced objects
By ImAFycus in forum New To JavaReplies: 3Last Post: 05-29-2010, 07:58 PM -
Sorting data in a Vector
By SBL in forum AWT / SwingReplies: 11Last Post: 11-27-2009, 03:20 AM -
How to store data from textfile to vector and delete a selected row.
By nemesis in forum New To JavaReplies: 15Last Post: 11-13-2009, 11:00 AM -
Reset form button and duplicated data
By dbashby in forum New To JavaReplies: 7Last Post: 10-10-2009, 04:40 AM -
XML Parsing, strange empty/duplicated Nodes
By Singing Boyo in forum Advanced JavaReplies: 0Last Post: 08-18-2009, 06:57 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks