Lab 9: Working with iterators
Goals: The goals of this lab are to practice designing an iterable data structure.
Submission: Submit your solution to Problem 1 on handins. This should include the ListOfLists<T> class and any classes required to make ListOfLists<T> iterable. This should include an examples class with tests for all of the methods you designed. This is due Tuesday Match 21st at 11:59pm.
Reminder: both partners should work on this together.
Problem 1: Iterators
Create a class ListOfLists<T> that stores an ArrayList of ArrayList<T>. This class must have the following methods:
A method void addNewList() that adds a new empty ArrayList<T> to the end of the list-of-lists.
A method void add(int index,T object) that adds the provided object to the end of the ArrayList<T> at the provided index in the list-of-lists. If the index is invalid, this method should throw an IndexOutOfBoundsException with a suitable message.
A method ArrayList<T> get(int index) that returns the ArrayList<T> at the provided index in the list-of-lists. If the index is invalid, this method should throw an IndexOutOfBoundsException with a suitable message.
A method int size() that returns the number of lists in this list-of-lists.
Now make the ListOfLists<T> iterable. 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; } }