Sunday, 1 September 2013

Why do these two list Iterators give different result?

Why do these two list Iterators give different result?

I am practicing list iteration then i got stuck. My question is, why do
these two methods give different results.
The first code prints out a infinite loop. While the second, prints out
the next String in the index.
I am new to java, this is also my first language.
package javaapplication115;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
package javaapplication115;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class JavaApplication115 {
public static void main(String[] args) {
String[] hi = {"yo", "wat", "sup"};
List<String> l1 = new ArrayList(Arrays.asList(hi));
while (l1.iterator().hasNext()) {
System.out.println(l1.iterator().next());
;
}
}
}
VS
package javaapplication115;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class JavaApplication115 {
public static void main(String[] args) {
String[] hi = {"yo", "wat", "sup"};
List<String> l1 = new ArrayList(Arrays.asList(hi));
Iterator<String> rator = l1.iterator();
while (rator.hasNext()) {
System.out.println(rator.next());
}
}
}

No comments:

Post a Comment