Re: How to loop through a list while inside the loop, the list size may be decreased?

Discussion in 'Java' started by Roedy Green, Sep 12, 2008.

  1. Roedy Green

    Roedy Green Guest

    On Fri, 12 Sep 2008 12:18:51 -0400, www <> wrote, quoted
    or indirectly quoted someone who said :

    > I want to delete the object from the list.


    if you use Iterator.remove, the loop will just run through the
    remaining elements without you needing to do any index calculations.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Sep 12, 2008
    #1
    1. Advertising

  2. Roedy Green

    zerg Guest

    Re: How to loop through a list while inside the loop, the list sizemay be decreased?

    www wrote:
    > Roedy Green wrote:
    >
    >>
    >> if you use Iterator.remove, the loop will just run through the
    >> remaining elements without you needing to do any index calculations.

    >
    > Thank you. I think that will work. But I have one more question: if I
    > want to use Java 5 fancy looping feature(avoid using iterator,
    > typecasting):
    >
    > for(Person p : listOfPerson)
    > {
    > if(p.getName() ...) //checking
    > {
    > ??? //My question: how can I remove the Person obj from
    > listOfPerson
    > }
    >
    > }
    >
    > I know how to do it by using iterator. But since the new loop feature of
    > Java 5 is cool, I am curious if it is able to do it.


    I don't think so. I think you have to have an explicit Iterator object
    to do that.
     
    zerg, Sep 12, 2008
    #2
    1. Advertising

  3. Re: How to loop through a list while inside the loop, the list sizemay be decreased?

    www wrote:
    > Roedy Green wrote:
    >
    >>
    >> if you use Iterator.remove, the loop will just run through the
    >> remaining elements without you needing to do any index calculations.

    >
    > Thank you. I think that will work. But I have one more question: if I
    > want to use Java 5 fancy looping feature(avoid using iterator,
    > typecasting):
    >
    > for(Person p : listOfPerson)
    > {
    > if(p.getName() ...) //checking
    > {
    > ??? //My question: how can I remove the Person obj from
    > listOfPerson
    > }
    >
    > }


    To avoid a ConcurrentModificationException you could do something like this:

    ----------------------------- 8< -----------------------------------
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;

    /**
    * @author RedGrittyBrick
    */
    public class ListTest {
    public static void main(String[] args) {

    List<String> list = new ArrayList<String>();
    list.addAll(Arrays.asList("a","b","c","d","e"));

    // Traverse list and remove b's.
    for (String s: new ArrayList<String>(list)) {
    System.out.print(s);
    if (s.equals("b"))
    list.remove(s);
    }
    System.out.println();

    // Print remaining list
    for (String s: list) {
    System.out.print(s);
    }
    System.out.println();

    }
    }
    ----------------------------- 8< -----------------------------------
    abcde
    acde

    But I suspect this may not be a good idiom!


    --
    RGB
     
    RedGrittyBrick, Sep 12, 2008
    #3
  4. www wrote:
    > Roedy Green wrote:
    >
    >>
    >> if you use Iterator.remove, the loop will just run through the
    >> remaining elements without you needing to do any index
    >> calculations.

    >
    > Thank you. I think that will work. But I have one more question: if
    > I
    > want to use Java 5 fancy looping feature(avoid using iterator,
    > typecasting):
    > for(Person p : listOfPerson)
    > {
    > if(p.getName() ...) //checking
    > {
    > ??? //My question: how can I remove the Person obj from listOfPerson
    > }
    >
    > }
    >
    > I know how to do it by using iterator. But since the new loop
    > feature
    > of Java 5 is cool, I am curious if it is able to do it.


    To use the remove() method, you need an explict iterator. Hoewever,
    you can still avoid typecasting, e.g.

    List<Person> list;
    for (Iterator<Person> iter = list.iterator(); iter.hasNext(); )
    {
    Person p = iter.next();
    if(p.getName() ...) //checking
    {
    iter.remove();
    }
    }
     
    Mike Schilling, Sep 13, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,289
    Tim Slattery
    Mar 30, 2005
  2. JJ
    Replies:
    3
    Views:
    409
    OHM \( One Handed Man \)
    May 8, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    522
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Big George

    Decreased number of posts on this forum

    Big George, Jan 16, 2013, in forum: ASP .Net
    Replies:
    3
    Views:
    661
    Arne Vajhøj
    Jan 17, 2013
  5. Isaac Won
    Replies:
    9
    Views:
    454
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page