Iterator Worksheet
By Mickey Arnold
starstarstarstarstarstarstarstarstarstar
Last updated 10 months ago
4 Questions
1 point
1
Question 1
1.
What is output by the code at right?
ArrayList<Integer> x = new ArrayList<Integer>();x.add(11);x.add(9);Iterator<Integer> it = x.iterator();System.out.println(it.next());System.out.println(it.next());
What is output by the code at right?
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(11);
x.add(9);
Iterator<Integer> it = x.iterator();
System.out.println(it.next());
System.out.println(it.next());
1 point
1
Question 2
2.
What is output by the code at right?
ArrayList<Integer> w = new ArrayList<Integer>();w.add(4);w.add(6);w.add(8);Iterator<Integer> iter = w.iterator();while(iter.hasNext()){ System.out.println(iter.next()); iter.remove();}System.out.println(w);
What is output by the code at right?
ArrayList<Integer> w = new ArrayList<Integer>();
w.add(4);
w.add(6);
w.add(8);
Iterator<Integer> iter = w.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
iter.remove();
}
System.out.println(w);
1 point
1
Question 3
3.
What is output by the code at right?
ArrayList<Integer> z;z = new ArrayList<Integer>();z.add(3);z.add(7);z.add(3);z.add(7);z.add(9);z.add(5);System.out.println(z);Iterator<Integer> itera = z.iterator();while(itera.hasNext()){ if(itera.next().compareTo(7)==0) itera.remove();}System.out.println(z);
What is output by the code at right?
ArrayList<Integer> z;
z = new ArrayList<Integer>();
z.add(3);
z.add(7);
z.add(3);
z.add(7);
z.add(9);
z.add(5);
System.out.println(z);
Iterator<Integer> itera = z.iterator();
while(itera.hasNext()){
if(itera.next().compareTo(7)==0)
itera.remove();
}
System.out.println(z);
1 point
1
Question 4
4.
What is output by the code at right?
ArrayList<Integer> a = new ArrayList<Integer>();a.add(5);a.add(6);a.add(9);a.add(7);a.add(2);ListIterator<Integer> iterator = a.listIterator();iterator.next();iterator.set(1);iterator.next();iterator.set(4);iterator.previous();iterator.remove();iterator.previous();iterator.set(0);System.out.println(a);
What is output by the code at right?
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(5);
a.add(6);
a.add(9);
a.add(7);
a.add(2);
ListIterator<Integer> iterator = a.listIterator();
iterator.next();
iterator.set(1);
iterator.next();
iterator.set(4);
iterator.previous();
iterator.remove();
iterator.previous();
iterator.set(0);
System.out.println(a);