Results 1 to 9 of 9
  1. #1
    brian.baert is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default ArrayList problem

    Hi,

    I am using an ArrayList wich contains ArrayList objects, I am now trying to address a specific object and change the values from 0 to 1, but with the code I have alle the elements are changed and not only the specific line ...
    Can anyone help me?

    This is the code fragment:

    if((rnr >= d) && ((n-1-d) >= rnr))
    {
    ArrayList test = kamerIndeling.get(rnr-1);
    for (int i=0; i<m; i++)
    {
    test.set(i, 1);
    }
    kamerIndeling.set(2, test);
    }

    rnr = a random number,
    kamerIndeling is a ArrayList with ArrayList objects

    Thx!

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

    Default

    Quote Originally Posted by brian.baert View Post
    Java Code:
                    if((rnr >= d) && ((n-1-d) >= rnr))
                    {
                       ArrayList test = kamerIndeling.get(rnr-1);
                       // ..
                       kamerIndeling.set(2, test);
                    }
    ... and now element #2 is the same as element rnr-1 (both entries point to the same ArrayList). Is this what you want?

    kind regards,

    Jos

  3. #3
    brian.baert is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    I am trying to get the element at that position, put it in a ArrayList en replace all elements of that list with 1.

    So I came up with a new part off code:

    if((rnr >= d) && ((n-1-d) >= rnr))
    {
    ArrayList test = kamerIndeling.get(rnr-1);
    for (int i=1; i<m-1; i++)
    {
    test.set(i, 1);
    }
    kamerIndeling.set(rnr-1, test);
    }


    But it still replaces too much instead of just the specific element of the outer ArrayList.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Do you want to change the value in a specific location? Or in the whole list?

    What's this?

    Java Code:
    for (int i=1; i<m-1; i++)

  5. #5
    brian.baert is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    In a specific location

    the for-loop is meant to get each sub-element between two borders.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    brian.baert is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    test.set(i, 1) has to replace the previous value by 1 (test is a ArrayList element of the main ArrayList)

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    brian.baert is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Problem with constructing an ArrayList
    By Diplo in forum New To Java
    Replies: 5
    Last Post: 12-07-2009, 03:52 PM
  2. Hey! ArrayList problem here
    By Samgetsmoney in forum New To Java
    Replies: 31
    Last Post: 02-20-2009, 12:39 AM
  3. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 06:22 PM
  4. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 05:46 AM
  5. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM

Posting Permissions

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