Documente Academic
Documente Profesional
Documente Cultură
An ordered list keeps the items in order no matter when they are added.
So the evolution of the list is as follows:
Operation List
X.add(new Integer(20)); 20
X.add(new Integer(35)); 20 35
X.add(new Integer(15)); 15 20 35
X.add(new Integer(10)); 10 15 20 35
X.add(new Integer(25)); 10 15 20 25 35
X.add(new Integer(45)); 10 15 20 25 45
EX 6.2. Given the resulting list X from Exercise 6.1, what would be the result of
each of the following?
a. X.first();
b. z = X.contains(new Integer(15));
X.first();
c. Y = X.remove(new Integer(10));
X.first();
X.first() returns 10
z = X.contains(new Integer(25)); z takes on true
X. first (); returns 10
Y = X.remove(new Integer(45)); Y takes on 45
X.first(); returns 10
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 6
EX 6.5. Compare and contrast ordered, unordered and indexed lists in terms of
adding elements in them.
The differences between ordered and unordered lists generally center on how
elements are added to the list. In an ordered list, we need only specify the
new element to add it. Its position in the list is based on its key value.
An unordered list supports three variations of the add operation. Elements can
be added to the front of the list, to the rear of the list, or after a particular
element that is already in the list.
In case of an indexed list, a new element can be inserted at a particular index,
or it can be added to the rear of the list without specifying an index at all.
When an element is inserted, the elements at higher indexes are shifted up to
make room.
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 6
Operation List
X.addToFront(new
Integer(20)); 20
X.addToRear(new
Integer(35)); 20 35
X.addToFront(new
Integer(10)); 10 20 15 35
X.addToRear(new
Integer(25)); 10 20 15 35 25
X.addAfter(new
Integer(45), new 10 20 15 45
Integer(15));
EX 6.7. Discuss the benefits provided by the private method find in the ArrayList
class. What is the logic behind keeping this method as private?
The find method helps in code reuse. It simplifies the definition of the
already complex remove method, as this method uses the find method in
implementing its algorithm. The find method is also used to implement the
contains operation as well as the addAfter method for an
ArrayUnorderedList.
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Java Software Structures, 4th Edition Exercise Solutions, Ch. 6
©2014 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.