7

Basically, the user submits a String which the Iterator searches an ArrayList for. When found the Iterator will delete the object containing the String.

Because each of these objects contain two Strings, I am finding trouble writing these lines as one.

Friend current = it.next();
String currently = current.getFriendCaption();

Thanks for any help!

Nayrdesign
  • 111
  • 1
  • 1
  • 2

1 Answers1

38

You don't need them on one line, just use remove to remove an item when it matches:

Iterator<Friend> it = list.iterator();
while (it.hasNext()) {
    if (it.next().getFriendCaption().equals(targetCaption)) {
        it.remove();
        // If you know it's unique, you could `break;` here
    }
}

Full demo:

import java.util.*;

public class ListExample {
    public static final void main(String[] args) {
        List<Friend>    list = new ArrayList<Friend>(5);
        String          targetCaption = "match";

        list.add(new Friend("match"));
        list.add(new Friend("non-match"));
        list.add(new Friend("match"));
        list.add(new Friend("non-match"));
        list.add(new Friend("match"));

        System.out.println("Before:");
        for (Friend f : list) {
            System.out.println(f.getFriendCaption());
        }

        Iterator<Friend> it = list.iterator();
        while (it.hasNext()) {
            if (it.next().getFriendCaption().equals(targetCaption)) {
                it.remove();
                // If you know it's unique, you could `break;` here
            }
        }

        System.out.println();
        System.out.println("After:");
        for (Friend f : list) {
            System.out.println(f.getFriendCaption());
        }

        System.exit(0);
    }

    private static class Friend {
        private String friendCaption;

        public Friend(String fc) {
            this.friendCaption = fc;
        }

        public String getFriendCaption() {
            return this.friendCaption;
        }

    }
}

Output:

$ java ListExample 
Before:
match
non-match
match
non-match
match

After:
non-match
non-match
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I understand your answer and thanks very much, the issue is that when I type `if(it.next().contains(text)) {` It doesn't work? I need to only search a certain part (String caption) of each object in the ArrayList. – Nayrdesign Nov 17 '11 at 22:27
  • @Nayrdesign: Be sure you're declaring the `Iterator` correctly, and that you're treating what it returns correctly. For instance, your example there `if (it.next().contains(text)) {` is acting like the `Iterator` is iterating over strings, but your question makes it look like the `ArrayList` contains `Friend` objects, not strings. The full demo shows how to do it correctly. Key bit is declaring `Iterator` so the `Iterator` is iterating over `Friend` instances, so that `it.next()` will be a `Friend`, then you can do `if (it.next().getFriendCaption().contains(text)) {`. – T.J. Crowder Nov 18 '11 at 10:04
  • @T.J.Crowder I modeled my program exactly after yours but I am getting: Exception in thread "main" java.util.NoSuchElementException at java.util.AbstractList$Itr.next(AbstractList.java:350) at RandomInt.messAroundWithListAgain(RandomInt.java:74) at RandomInt.main(RandomInt.java:85) – hologram Nov 30 '12 at 19:03
  • @dyoverdx: I don't know what to tell you, clearly it's not modelled *exactly* like the above, as the above works. Perhaps ask a question, quoting a complete but small and self-contained example demonstrating the problem you're having, and folks will be able to help you. – T.J. Crowder Nov 30 '12 at 22:43