[ Pobierz całość w formacie PDF ]

their elements. They can have duplicates and because their lookup key is the position and not some hash code,
every element can be modified while they remain in the list.
Table 9-1 lists the methods of the List interface. Added to those in the Collection interface are methods
supporting element access by position or order.
Table 9-1: Summary of the List Interface
VARIABLE/METHOD VERSION DESCRIPTION
NAME
add() 1.2 Adds an element to the list.
addAll() 1.2 Adds a collection of elements to the list.
clear() 1.2 Clears all elements from the list.
contains() 1.2 Checks to see if an object is a value within the list.
containsAll() 1.2 Checks if the list contains a collection of elements.
equals() 1.2 Checks for equality with another object.
107
What's New
get() 1.2 Returns an element at a specific position.
hashCode() 1.2 Computes hash code for the list.
indexOf() 1.2 Searches for an element within the list.
isEmpty() 1.0 Checks if the list has any elements.
iterator() 1.2 Returns an object from the list that allows all of the list's elements
to be visited.
lastIndexOf() 1.2 Searches from the end of the list for an element.
listIterator() 1.2 Returns an object from the list that allows all of the list's elements
to be visited sequentially.
remove() 1.2 Clears a specific element from the list.
removeAll() 1.2 Clears a collection of elements from the list.
retainAll() 1.2 Removes all elements from the list not in another collection.
set() 1.2 Changes an element at a specific position within the list.
size() 1.2 Returns the number of elements in the list.
subList() 1.2 Returns a portion of the list.
toArray() 1.2 Returns the elements of the list as array.
You'll find three concrete list implementations in the Collections Framework: Vector, ArrayList, and
LinkedList. The Vector class from the historical group has been reworked into the framework and was
discussed in Chapter 3. ArrayList is the replacement for Vector in the new framework. It represents an
unsynchronized vector where the backing store is a dynamically growable array. On the other hand is
LinkedList, which, as its name implies, is backed by the familiar linked list data structure.
Note There is no predefined sorted List implementation ( TreeList ). If you need to maintain the elements in a
sorted manner, consider either maintaining your elements in a different collection or sorting the elements
after you are done inserting (into another collection type).You can manually keep the elements in a
LinkedList in a sorted manner. However, insertion becomes a very costly operation, as you must
manually traverse the list before each insertion.
What's New
Looking quickly at the list doesn't reveal what's added to the List interface as compared to the Collection
interface it extends. Table 9-2 highlights those new methods. You'll find some repeated methods from the
Collection interface. When also found in the Collection interface, the List version includes an additional
argument to specify position.
Table 9-2: Methods Added to List Interface
VARIABLE/METHOD VERSION DESCRIPTION
NAME
add() 1.2 Adds an element to the list at a specific index.
addAll() 1.2 Inserts a collection of elements to the list starting at a specific
position.
108
Usage Issues
get() 1.2 Returns an element at a specific position.
indexOf() 1.2 Searches for an element within the list.
lastIndexOf() 1.2 Searches from the end of the list for an element.
listIterator() 1.2 Returns an object from the list that allows all of the list's elements
to be visited sequentially.
remove() 1.2 Clears a specific element from the list.
set() 1.2 Changes the element at a specific position within the list.
subList() 1.2 Returns a portion of the list.
We'll examine all of the methods of List in detail as we look at the specific implementations.
Usage Issues
When using the List interface, one of the first problems or at least questions you may run across is the name
conflict with the java.awt.List class. How can you use both classes in the same program? Or, at a minimum,
how can you import both packages into the same program when you need to use only one of the two classes?
" Solution 1 Fully qualify everything: [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • littlewoman.keep.pl