Shuffling Arrays

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]

Poem.java
BallBox box = new BallBox(poem.length);

shuffledPoem = new String[poem.length];
for (int index = 0; index < poem.length; ++index) {
   shuffledPoem[index] = poem[box.nextBall()];
}

*Shuffling keeps all components of an array but puts them into some random order.
 
*poem is an array that stores a poem, using a String component for each line.
 
*length is a public attribute of all arrays that represents the length of an array. This is the number of lines in this example.
 
*We set up a ballbox containing all indices of poem from 0 to poem.length - 1.
 
*Now we draw all balls in random order and fill shuffledPoem subsequently with those lines of poem that correspond to the ball drawn.
 

 [Previous Chapter]  [Previous Page]  [Contents]  [Next Page]  [Next Chapter]
Copyright © 2001, 2002 Andreas Borchert, converted to HTML on February 11, 2002