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.