Data Structures and Algorithms in Java
Authors: Drake P.
Published year: 2004
Pages: 49-51/216
Buy this book on amazon.com >>

[Page 153 ( continued )]

Summary

The size of an array cannot be changed, but the array-based structures discussed in this chapter are able to grow and shrink. They do this by maintaining a separate field indicating how many of the array positions are currently in use. This field also provides the index of the next available position. If all of the positions are in use, an array-based structure can be stretched by copying all of its elements into a new, larger array.


[Page 154]

These techniques for stretching and shrinking are used in the ArrayStack class. The ArrayQueue class must do a little more work because, as elements are added to the back and removed from the front, the in-use portion of the array marches to the right. When it hits the right end of the array, it wraps around to the beginning. This is accomplished using the % operator.

The List interface describes a much more general data structure. It is implemented by the ArrayList class. In addition to providing basic methods for accessing particular elements and so on, a List can return an Iterator. An Iterator allows us to traverse the List, visiting each element.

Java's collections framework, in the java.util package, includes a List interface, an ArrayList class, a Stack class, and a Queue interface. We will see more interfaces and classes from this framework in later chapters.



[Page 154 ( continued )]

Vocabulary

abstract class

Class that cannot be instantiated but may contain abstract methods. Unlike an interface, an abstract class can also contain fields and nonabstract methods .



abstract method

Method signature with no body, to be provided by another class. Found in interfaces and abstract classes.



backward compatibility

Of a compiler or other system, ability to work with old software.



iterator

Object allowing us to traverse a data structure. In Java, an instance of the java.util.Iterator class.



Java collections framework

Set of classes in the java.util package descending from Collection. These are standard implementations of many common data structures.



traverse

Go through a data structure, visiting each element.



visit

Do something with or to an element of a data structure.





[Page 154 ( continued )]

Problems

5.31

In the Card class, replace the constant ACE with two constants ACE_LOW=1 and ACE_HIGH=14 . Make Deck abstract and provide two subclasses AceLowDeck (which has low aces) and AceHighDeck (which has high aces).

5.32

Rewrite the ArrayQueue class (Figure 5-23) so that, instead of a field size indicating the number of Objects in the queue, there is a field back indicating the next available position. How will you know when the queue is full?

5.33

Rewrite the ArrayQueue class (Figure 5-23) so that the remove() method shifts all of the data to the left, keeping the front of the queue at position 0. This allows the field front to be removed. Which methods become simpler as a result of this change? Which become more complicated?


[Page 155]
5.34

Define a class ReverseArrayIterator, which implements java.util.Iterator but visits an ArrayList's elements in reverse order. Add a method reverseIterator() to the ArrayList class. The method returns an instance of ReverseArrayIterator.

5.35

Write a Deck class whose only field is of type ArrayList<Card>. Use the static shuffle() method in the API for the Collections class (which should not be confused with the Collection interface).


Data Structures and Algorithms in Java
Authors: Drake P.
Published year: 2004
Pages: 49-51/216
Buy this book on amazon.com >>

Similar books on Amazon