Results 1 to 16 of 16
  1. #1
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default 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. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    // initialize x1,x2,x3 from some function

    Thats the important part. Show us this method!

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,926
    Rep Power
    8

    Default

    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by khlitoshi View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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.

  7. #7
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default 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
    Attached Files Attached Files

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by khlitoshi View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    yes, that's the purpose.
    would you please explain what is wrong.

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    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.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by khlitoshi View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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 ?

  13. #13
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    I just notice your post sorry, I'll do what u said

  14. #14
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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

  15. #15
    khlitoshi is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

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

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by khlitoshi View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. ArrayList duplicated/replaced objects
    By ImAFycus in forum New To Java
    Replies: 3
    Last Post: 05-29-2010, 07:58 PM
  2. Sorting data in a Vector
    By SBL in forum AWT / Swing
    Replies: 11
    Last Post: 11-27-2009, 03:20 AM
  3. Replies: 15
    Last Post: 11-13-2009, 11:00 AM
  4. Reset form button and duplicated data
    By dbashby in forum New To Java
    Replies: 7
    Last Post: 10-10-2009, 04:40 AM
  5. XML Parsing, strange empty/duplicated Nodes
    By Singing Boyo in forum Advanced Java
    Replies: 0
    Last Post: 08-18-2009, 06:57 AM

Posting Permissions

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