Chapter 11

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Appendix C.  Answers to Review Questions


A1:

An array is a structure built into Java, whereas an ArrayList is a class. More importantly an array is a fixed size, whereas an ArrayList can grow and shrink as needed.

A2:

A Vector is thread-safe; meaning that multiple threads of execution can access the Vector at the same time without clobbering each other's data.

A3:

A hash table is faster at inserting and retrieving data than a tree, so if this reflects the nature of your data, a hash table is the best choice.

A4:

Hash table collision occurs when two objects compute the same hash code; this is resolved by applying another algorithm on one of the objects to compute a new unique hash value.

A5:

The search operation on a tree is fast (defined the worst case time as the natural logarithm of the number of elements in the tree) because object-insertion operations into and deletion operations from a tree take care to "balance" the tree. This ensures that there are approximately the same number of child nodes to the left of any node as there are to the right.

A6:

The definition of a Set precludes it from containing duplicate values.

A7:

A HashMap maintains its keys in a hash table while a TreeMap maintains its keys in a tree. Trees are sorted, whereas hash tables are not, so the iterated output of a TreeMap will be sorted, but the output of a HashMap most likely won't.

A8:

A stack is a data structure in which the first item added to the Stack is the last object removed and the last item added is the first object removed. This is explained in the acronym LIFO: Last In, First Out. It is similar in concept to piling books one on top of the other: You can only see the book on the top of your stack.

A9:

Although a stack defines the retrieval of its elements as last in, first out (LIFO), a queue works the opposite, it is first in, first out (FIFO). A stack is similar to a stack of books: You can only see the book on the top of your stack, whereas a queue is similar to a line in the supermarket: The first person in line checks out first.

A10:

An iterator is a generic interface used for traversing the values stored in one of Java's collection classes. It has three simple methods:

hasNext(): Are there any more elements in the collection?

next(): Returns the next element in the collection.

remove(): Removes the last element returned by the next() method call from the underlying collection.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net