Reverse.java


Below is the syntax highlighted version of Reverse.java from §1.5 Input and Output.


/******************************************************************************
 *  Compilation:  javac Reverse.java StdIn.java
 *  Execution:    java Reverse
 *  Dependencies: StdIn.java StdOut.java
 *
 *  Reads in real numbers and prints them in reverse order.
 *  Uses an array.
 *
 *  % java Reverse
 *  1.0 2.0 3.0 4.0 5.0 6.0 5.9 5.8 5.7
 *  <Ctrl-d>
 *  5.7 5.8 5.9 6.0 5.0 4.0 3.0 2.0 1.0
 *
 *  Windows users: use <Ctrl-z> instead of <Ctrl-d> to signify EOF.
 *
 ******************************************************************************/

public class Reverse {
    public static void main(String[] args) {
        double[] data = StdIn.readAllDoubles();
        for (int i = data.length-1; i >= 0; i--)
            StdOut.print(data[i] + " ");
        StdOut.println();
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:14:17 EDT 2022.