Chapter 4

Section 4.1

1.

//start size 5

myList.add("Pokey"); //adds element to end of list, size now at 6

myList.add("Campy"); //adds element to end of list, size now 7

int i = myList.indexOf("Happy"); //returns integer 3 to i

myList.set(i, "Bouncy"); //sets element i (3) to "Bouncy". Size = 7

myList.remove(myList.size() - 2); //Removes item 5 ("Pokey"). Size = 6

String temp = (String) myList.get(1); //Stores "Doc" in temp. Size = 6

myList.set(1, temp.toUpperCase()); //Sets item 1 to "DOC",

// replacing "doc".

//Final size = 6

Section 4.2

1.

ArrayList myList = new ArrayList(); //creates new array list

myList.add(new Double(3.456)); //adds Double to the list

myList.add(new Integer(5)); //adds Integer to the list

double result = ((Integer) myList.get(1)).intValue

+ ((Double) myList.get(0)).doubleValue;

//Adds the two numbers stored.

System.out.println("Result is " + result); // Result is 8.456

Section 4.3

1.

Array contents at end of first loop: 0,1,2,3,3,3,3,3

Array contents at end of second loop 0,1,2,3,3,4,5,6

2.

/******************************************************

Removes middle object and re-inserts is at the end

@return Object object removed and re-inserted

*/

public Ojbect removeMiddleInsertEnd(){

int index = (int) Math.ceil(size/2);

Object returnValue = remove(index);

add(size, returnValue);

return returnValue

}

Section 4.4

1.

int sum = 0;

Node nodeRef = head;

while (nodeRef != null) {

if (nodeRef.data instanceof Integer) {

int next = ((Integer) nodeRef.data).intValue();

sum += next;

}

nodeRef = nodeRef.next;

}

2.

Data and next node.

Data, previous and next node.

Its predecessor.

Node itself.

3.

Inserts a new first node (referenced by head) storing a data object of type String, value = “Shakira”. Its next field references the old list head.

Effectively removes the third node in the list by referencing the second node in the list (nodeRef) and resetting its next data field to the next data field of the third node.

Inserts a new node at the end of a list storing a data object of type String, value = “Tamika”.

Looks in a linked list for a node with data object of type String with a value of “Harry”. If this node is found, the code replaces the node’s data with a String of value “Sally”. Then it nserts a new node directly after this node, with the String value “Harry” and a next field referencing the successor of the node that followed "Harry", effectively replacing the successor of "Harry". This would cause a NullPointerException if "Harry" has no successor.

4.

Removes node containing “Harry”.

Inserts node “Tamika” at the head of the list.

Inserts node “Shakira” immediately after the first node (head) of the list.

Section 4.5

1.

int indexOfSam = myList.indexOf("Sam"); // returns 3

ListIterator iteratorToSam = listIterator(indexOfSam);

//Returns an iterator between elements 2 and 3

iteratorToSam.previous(); // returns “Harry”

iteratorToSam.remove(); // Removes “Harry”

2.

int indexOfSam = myList.indexOf("Sam"); // returns 3

ListIterator iteratorToSam = listIterator(indexOfSam);

//Returns an iterator between elements 2 and 3

iteratorToSam.next(); // returns “Sam”

iteratorToSam.remove(); // Removes “Sam”

3. If we omit the iteratorToSam.previous() call, we will get an IllegalStateException.

Section 4.6

1. The first condition would cause a NullPointerException if nextItem was null. Short-circuit evaluation prevents this from happening in the actual method.

2. By calling next or previous, lastItemReturn references an existing node instead of null so the code can perform action regarding that node.

3. The Java API specification states that we will get an IllegalStateException. We did not implement method remove, but since add sets lastItemReturned to null, we could certainly test for this and throw an IllegalStateException in remove.

Section 4.7

1. We don't extend LinkedList because we want to be able to control the ordering of the items being in the list. If we extend the LinkedList class, the user can invoke the set and add methods of the LinkedList class, which could result in items being inserted out of order.

2. Other methods: clear, contains, containsAll, equals, hashCode, indexOf, isEmpty, lastIndexOf, remove(index), removeAll, retainAll, subList, toArray.

3. This is a potential problem. The returned Iterator object can be downcast back to a ListIterator, and then the add method applied to modify the OrderedList.

Section 4.8

1. Methods iterator and size are abstract in the Java API documentation for the AbstractCollection definition. No, we can't use KWArrayList and extend AbstractCollection because class KWArrayList does not have an iterator method. Yes, we could use the KWLinkedList class and extend the AbstractCollection.