Jason Yu
Jason Yu

Reputation: 35

Returning an Iterator from method

I can't seem to print the elements in the Iterator when returned from the method removeTwos(). I am trying to remove elements from the list that only has two characters.

public class Main {

    public static void main(String[] args) {

        // write your code here
        List<String> list = new ArrayList<>();
        list.add("hi");
        list.add("what");
        list.add("who");
        list.add("ok");

        System.out.println(removeTwos(list));
    }

    public static String removeTwos(List<String> stringList) {

        Iterator<String> itr = stringList.iterator();
        for(int i = 0; i < stringList.size(); i++) {
            if(itr.hasNext() && itr.next().length() == 2) {
                itr.remove();
                System.out.println(itr.toString());
            }
        }
        return itr.toString();
    }
}

Thanks.

Upvotes: 2

Views: 100

Answers (6)

Bloodeye
Bloodeye

Reputation: 94

The thing is: you don't need an Iterator at all for what you're trying to do. You can just search each item in the list one by one with the methods of the list.

Try this code out and see if it works for you:

public class JavaApplication255 {

    public static void main(String[] args) {
        // write your code here
        List<String> list = new ArrayList<>();
        list.add("hi");
        list.add("what");
        list.add("who");
        list.add("ok");

        removeTwos(list);
        System.out.println(list);
    }

    public static void removeTwos(List<String> stringList){

        for(int i = stringList.size() - 1; i >= 0 ; i--){
            String string = stringList.get(i);

            if (string.length() == 2){
                stringList.remove(string);
            }
        }
    } 
}

Upvotes: 1

0xCursor
0xCursor

Reputation: 2268

To use an Iterator for this problem, you can edit your removeTwos method to something like this:

public static String removeTwos(List<String> stringList) {

    Iterator<String> itr = stringList.iterator();
    while (itr.hasNext()) {
        String value = itr.next();
        if (value.length() == 2) {
            System.out.println(value);
            itr.remove();
        }
    }

    return stringList.toString();
}

When using an Iterator, it is safe to remove elements from the list while looping through it. Here's a link that demonstrates that it's safe.

Upvotes: 1

hhrzc
hhrzc

Reputation: 2050

public static String removeTwos(List<String> stringList){
    return stringList.stream()
            .filter(p -> p.length() != 2)
            .collect(Collectors.joining(", "));
}

return:

what, who

Upvotes: 0

RaffleBuffle
RaffleBuffle

Reputation: 5455

I like using for loops with iterators:

public static void removeTwos(List<String> stringList) 
{
    for (Iterator<String> itr = stringList.iterator(); itr.hasNext();)
        if (itr.next().length() == 2) itr.remove();
}

public static void main(String[] args)
{
    String[] arr = { "hi", "what", "who", "ok" };
    List<String> list = new ArrayList<>(Arrays.asList(arr));
    removeTwos(list);
    System.out.println(list);
}   

Output:

[what, who]

Upvotes: 0

mavriksc
mavriksc

Reputation: 1132

using stream and filter is easy 1 liner unless you need to use iter.

List<String> noTwos = list.stream().filter(s-> s.length() != 2).collect(Collectors.toList());

Upvotes: 1

Whome
Whome

Reputation: 10400

If you want to use for(int idx..) loop and also remove items you should iterate from end to start.

for(int idx=stringList.size()-1; idx>=0; idx--){
   String val = stringList.get(idx);
   if (val.length()==2)
      stringList.remove(idx);
}
...later if you want to print a list..
for(int idx=0; idx<stringList.size(); idx++) {
   System.out.println( String.format("%d:%s", idx, stringList.get(idx)) );

Upvotes: 0

Related Questions