HOMEWORK 1
Due Date: 26th June 10 pm
QUESTIONS
- Let list1 and list2 contain arbitrary number of items. Describe briefly what the following code snippet does:
while (!list1.isEmpty()) {
list2.add(list2.size(), list1.remove(0));
}
- Suppose the design requirement for the following code is to remove every other item from a given list.
for (int i = 0; i < colors.size(); i += 2) {
colors.remove(i);
}
- If colors initially contains ["violet", "indigo", "blue", "green", "yellow", "orange", "red"], then trace the code on this input by showing the value of i, value of colors.size() and contents of colors for each execution of the for loop body. To receive full credit, you should start by showing the initial values before the loop execution begins and then show results after each execution of the for loop, until you show the final results. Use this format for your answer: i = unInitialized, colors.size() = ..., colors = [...] and then i = 0, ...
- Does the code work as per the design requirement? Explain your answer briefly.
- Assume the following:
- The Iterator<E> interface is as discussed in lecture.
- The ArrayList<E> class implements the Iterable<E> and ListADT<E> interfaces.
- The items in a list can be compared with the equals method or the compareTo method.
Complete the Java method specified below, making use of iterators Hint: You would need a lot of them!. Assume this method is implemented in a main class. To receive full credit, your solution:
- must explicitly use iterators for traversing lists
- must not use the contains method
- must not modify the contents of the parameter
public static <E> ArrayList<E> flatten(ArrayList<ArrayList<E>> list) {
// If list is null, throw a NullPointerException.
// If list is empty, return a new empty list.
// Otherwise create and return a new list that contains the objects contained in
// any of the lists contained in list, removing duplicate items. The order of
// items in the returned list should be the same as the order in which they
// first appear in the original list.
//Example: list: [[1,2,3], [4,3,6], [1,5]] result: [1,2,3,4,6,5]
SUBMISSION
Please refer to submission steps for submitting your work.