Java Reference
In-Depth Information
Sequences or Lists
array[0]
array[1]
array[2]
array[3]
Array or Vector
object
object
object
object
null
object
object
object
object
Linked List
next
next
next
next
last-in
Stack
object
object
object
object
first-out
first-out
first-in
object
object
object
object
Queue
Because a list is linear, you will only be able to add a new object to the list at the beginning, or at the
end, or inserted following a given object in sequence - after the fifth say. Generally, you can retrieve an
object from a list in several ways. You can select the first or the last; you can get the object at a given
position - as in indexing an array; or you can search for an object identical to a given object by
checking all the objects in the sequence either backwards or forwards. You can also iterate through the
list backwards or forwards accessing each object in sequence. We didn't implement all these capabilities
in our linked list class in Chapter 6, but we could have done.
You can delete objects from a list in the same sorts of ways that you retrieve an object; that is, you can
remove the first or the last, an object at a particular position in the sequence, or an object that is equal
to a given object. Sequences or lists have the facility that they can store several copies of the same object
at different places in the sequence. This is not true of all types of collections, as we will see.
A stack , which is a last-in first-out storage mechanism, is also considered to be a list, as is a queue ,
which is a first-in first-out mechanism. It is easy to see that a linked list can act as a stack, since using the
methods to add and remove objects at the end of a list makes the list operate as a stack. Similarly, only
adding objects by using the method to add an object to the end of a linked list, and only retrieving
objects from the head of the list, makes it operate as a queue.
Maps
A map is rather different from a set or a sequence because the entries involve pairs of objects. A map is also
referred to sometimes as a dictionary because of the way it works. Each object that is stored in a map has an
associated key object, and both the object and its key are stored as a pair. The key determines where the
object is stored in the map, and when you want to retrieve an object you must supply the appropriate key -
so it acts as the equivalent of a word that you look up in a regular dictionary.
Search WWH ::




Custom Search