Results 1 to 8 of 8
  1. #1
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default altering arraylist

    Hi. So I learned that when using a for each loop through an array list...I cant just straight up alter it. I have to create a new arraylist and alter that is this true? For example:

    I cannot do this: correct?
    Java Code:
    
    		for (Fish f: fish){ //removing element f from arraylist called fish
    			if(f.isAlive() == false){
    				fish.remove(f);l);
    instead I would have to do this right???.

    Java Code:
    ArrayList<Fish> f2; //create new arraylist
    		f2 = new ArrayList<Fish>(fish); //set tht equal to original
    		for (Fish f: fish){ //goes through original
    			if(f.isAlive() == false){
    				f2.remove(f); //copied is altered instead
    Am I doing this right?

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: altering arraylist

    first method you posted wouldnt compile.

    what were you trying to achieve using this: "l);" after a method call?

    The second method would work, but since you are creating a shallow copy, hence you have to pointers to the same object, if you remove something from t2 it will be removed from t1 (as far as i know) Is this what you want to achieve?
    Last edited by monkeyjr97; 05-09-2013 at 01:07 AM.

  3. #3
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: altering arraylist

    I want to remove Fish objects while iterating through the arraylist

  4. #4
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: altering arraylist

    I learned that I cant alterthe arraylist as I am interating throught it. So I had to create a new array and copy the original one so that as I am going through the original arraylist, I am removing elements from the copied arraylist.

  5. #5
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: altering arraylist

    if you want to remove elements from the COPIED ARRAYLIST ONLY, you will need a deep copy of the ArrayList.

  6. #6
    NoobieCode is offline Senior Member
    Join Date
    Mar 2013
    Posts
    123
    Rep Power
    0

    Default Re: altering arraylist

    My goal is to remove elements from array list. Im not sure what copy I want. so in my arraylist i have 3 Fish objects. If i remove 1. My arraylist will have 2. I know I cant just remove it when im using a for each loop. How will I do this exactly?

  7. #7
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: altering arraylist

    but if you have a shallow copy both arrayLists will point to the same memory location. So if i remove a fish from my arrayList copy it will also be deleted from the original.

    Your logic is correct for removing a fish, you just need to make a deep copy.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: altering arraylist

    I want to remove Fish objects while iterating through the arraylist
    Loop over the List in reverse from last element to first using a for loop, removing the items as needed, or loop over the List using a ListIterator

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. Replies: 22
    Last Post: 05-19-2010, 02:00 PM
  4. Problem altering text in JLabel
    By porchrat in forum New To Java
    Replies: 3
    Last Post: 06-29-2009, 12:37 AM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Tags for this Thread

Posting Permissions

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