2

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.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
Jason Yu
  • 35
  • 5

6 Answers6

1

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.

0xCursor
  • 2,242
  • 4
  • 15
  • 33
1

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());
mavriksc
  • 1,130
  • 1
  • 7
  • 10
1

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);
            }
        }
    } 
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
Bloodeye
  • 94
  • 6
  • 2
    Index-looping and removing at the same time should be done from back to start. – Whome Sep 12 '18 at 21:17
  • 1
    You're right that he doesn't need to use an `Iterator` for this problem. `Iterator` can be used as well - it's just personal preference. – 0xCursor Sep 12 '18 at 21:39
  • 1
    Definitely. It's just nicer to keep things simple. Thanks for the edit btw, have a nice day ! – Bloodeye Sep 12 '18 at 21:43
0

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)) );
Whome
  • 10,181
  • 6
  • 53
  • 65
  • How would I be able to display it in main? Or are we not supposed to use iterators to print or displaY? – Jason Yu Sep 12 '18 at 21:09
  • See my answer; i put a loop and sysout print example. You don't need to return a list object from the subroutine as you are in-place modifying the list content. – Whome Sep 12 '18 at 21:10
0

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]
RaffleBuffle
  • 5,396
  • 1
  • 9
  • 16
0
public static String removeTwos(List<String> stringList){
    return stringList.stream()
            .filter(p -> p.length() != 2)
            .collect(Collectors.joining(", "));
}

return:

what, who

Valentyn Hruzytskyi
  • 1,772
  • 5
  • 27
  • 59