Results 1 to 9 of 9
  1. #1
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default Having problems clearing an arrayList

    I m having a 2d arraylist and inside a loop i want to parse every 1d list of the 2d list to a temporary list. Also I want at the end of every iteration i want to clear this temporary list so to the next step to parse the i-est list of my initial 2d.

    The code its the following:

    Java Code:
       List<List<Integer>> conVert = new ArrayList<List<Integer>>();
       List<Integer> temp = new ArrayList<Integer>();
    
       for (int i = 0; i<conVert.size(); i++){
    
                temp.addAll(conVert.get(i));
                Collections.sort(temp);
                System.out.println(temp);
    
          for(int j = 0; j<temp.size(); j++){
    ...  several commands
    }
    temp.clear();
    }
    I have several commands which is not function well due to that temp doesnt clear properly. Any idea?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm not sure what you are trying to do. Is your goal to extract one element from the 2d ArrayList and store it in the temp list, sort the temp list, print it, and then clear it?

    Why not try to start small and build your way up.

    Simply store the list in the array, print temp, clear temp, print temp. This will let you see what's happening each time through the loop.

    Perhaps you have a logic error somewhere in snipped code in the inner loop.

    I wrote a quick program which fills a 2d array, extracts each item and stores it in temp, prints temp, then clears temp, then prints it again and all is well. Try writing something similar, maybe it will help you see what you are doing wrong?
    Last edited by sunde887; 05-05-2011 at 06:35 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    A List holds references to the contained objects, not the objects themselves. If you think you're re-using a List and adding it repeatedly, with differing content, to a List<List<...>> you're wrong. All you end up with is a List<List<...>> that contains several entries for the same List<...> and you'll get the same content at all indexes.

    db

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default

    ... or maybe that's not it at all.

    db <-- polishes crystal ball

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    I'm pretty sure therres a method in the ArrayList class called clear();

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ya, what he is trying to do is fuzzy to me and can definitely be clarified. He also cut out a section of code which could be causing the problem. Although I'm not really sure what the problem is.

  7. #7
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    Ok I hace the 2d arrayList conVert. I want in the loop

    (1) for (i=0; i=conVert.size(); i++){

    store temprorarily the conVert.get(i) in the arrayList temp

    After that to have another loop where I must calculate some things(100% is not the problem):

    (2) for(j=0; j<temp.size(); j++){

    }
    and here I want to clear temp so as to in the next iteration of the (1) for to parse the next conVert.get(i).

    The problem is that temp.clear() doesnt work so for example
    if coVert.get(0)=[1,3] and coVert.get(0)=[4,7] then the list temp in the first iter is temp = [1,3] and in the second iter temp = [1,3,4,7]. I want in the second iter to be temp = [4,7].


    }

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You must be misplacing the call to clear, that's why I was wondering what the inner loop looks like.

  9. #9
    zenitis's Avatar
    zenitis is offline Member
    Join Date
    May 2010
    Posts
    33
    Rep Power
    0

    Default

    yeah I put it in the beggining and it works like a charm, thanks guys.

Similar Threads

  1. How to implement boolen in this arraylist problems?
    By Vin5253 in forum New To Java
    Replies: 1
    Last Post: 02-07-2011, 04:31 AM
  2. Help Clearing BufferedImage
    By ketann in forum Java 2D
    Replies: 19
    Last Post: 03-29-2010, 07:45 PM
  3. Problems with a loop calling data from an ArrayList.
    By moriarty in forum New To Java
    Replies: 30
    Last Post: 03-28-2010, 02:00 AM
  4. ArrayList problems
    By komo225 in forum New To Java
    Replies: 4
    Last Post: 02-12-2009, 04:14 AM
  5. [Problems] ArrayList
    By Zuela in forum New To Java
    Replies: 1
    Last Post: 06-16-2008, 11:51 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
  •