Page 343 - AP Computer Science A, 7th edition
P. 343
/∗ ∗ Shuffle the Decks. ∗ / public void shuffleAll()
{
for (Deck d : allDecks) d.shuffle();
}
/∗ ∗ Write contents of all the Decks. public void printDecks()
{
for (Deck d : allDecks) d.writeDeck();
} }
NOTE
1. The statement
allDecks = new Deck[NUMDECKS];
∗ /
creates an array, allDecks, of 500 Deck objects. The default initialization for these Deck objects is null. In order to initialize them with actual decks, the Deck constructor must be called for each array element. This is achieved with the for loop of the ManyDecks constructor.
2. In the shuffleAll method, it’s OK to use a for-each loop to modify each deck in the array with the mutator method shuffle.
Analyzing Array Algorithms
Example 1
Discuss the efficiency of the countNegs method below. What are the best and worst case configurations of the data?
/∗ ∗ Precondition: arr[0],...,arr[arr.length–1] contain integers.