Reversing Arrays

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

Poem.java
/**
 * Reverse the order of the first count lines in the poem.
 *
 * @param  count   The number of lines to be reversed.
 */
public void reverse(int count) {
   // count is in [1..length], map that to [0..length-1]
   int position = count - 1;

   // reverse the order of the first position elements
   for (int index = 0; index <= position / 2; ++index) {
      String tmp = shuffledPoem[index];
      shuffledPoem[index] = shuffledPoem[position - index];
      shuffledPoem[position - index] = tmp;
   }
}

*This method reverses the order of the first count components of the array shuffledPoem.
 

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