0% found this document useful (0 votes)
2 views

Iterator Worksheet1 Java Aplus

The document contains a worksheet with code snippets that demonstrate the use of iterators in Java. It asks for the output of various blocks of code involving ArrayLists and iterators, including adding, removing, and modifying elements. Each question requires the reader to analyze the code and predict the resulting output.

Uploaded by

murtazaazm065
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Iterator Worksheet1 Java Aplus

The document contains a worksheet with code snippets that demonstrate the use of iterators in Java. It asks for the output of various blocks of code involving ArrayLists and iterators, including adding, removing, and modifying elements. Each question requires the reader to analyze the code and predict the resulting output.

Uploaded by

murtazaazm065
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 1

Name : ___________________________ Date : _________________

A+ Iterator Worksheet

Show the output of each block of code below.

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());

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);

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);

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);

© A+ Computer Science – Iterator Worksheet - www.apluscompsci.com

You might also like