On this page:
Problem 1: Iterators
8.5

Lab 11: Working with iterators

Goals: The goal of this lab is to practice designing an iterable data structure.

Submission: There will be some aliasing questions in handins to do during the lab. These will be graded on correctness. (Reminder that attendance in lab is required to submit lab work.) For extra credit you may submit your solution to Problem 1 in handins before the end of your lab. To earn extra credit, your solution must be correct and well-designed. These are individual submissions, but you may work with your partner on them.

Problem 1: Iterators

Create a class ListOfLists<T> that has a field that is an ArrayList of ArrayList<T> called lists. This class must have the following methods:

Now make the ListOfLists<T> iterable. The methods you implemented above should be helpful. The intended behavior should be as follows:

void testListOfLists(Tester t) {
ListOfLists<Integer> lol = new ListOfLists<Integer>();
//add 3 lists lol.addNewList();
lol.addNewList();
lol.addNewList();
 
//add elements 1,2,3 in first list lol.add(0, 1);
lol.add(0, 2);
lol.add(0, 3);
 
//add elements 4,5,6 in second list lol.add(1, 4);
lol.add(1, 5);
lol.add(1, 6);
 
//add elements 7,8,9 in third list lol.add(2, 7);
lol.add(2, 8);
lol.add(2, 9);
 
//iterator should return elements in order 1,2,3,4,5,6,7,8,9 int number = 1;
for (Integer num : lol) {
t.checkExpect(num, number);
number = number + 1;
}
}