0

Sort numbers random in array

import java.util.*;

class FillAndSort {
   public static void main( String[ ] args ) {
      if ( args.length != 1 ) {
        System.err.println( "java FillAndSort <number of doubles>" );
        return; //*** exits main and, therefore, the program stops
      }
      int n = Integer.parseInt( args[ 0 ] ); //** convert String to int
      double[ ] nums = new double[ n ];
      Random r = new Random();               //** construct a Random
      int i = 0;
      while ( i < n ) {                      //** fill with doubles
         nums[ i ] = r.nextDouble();         //** next random double
         i = i + 1;
      }
      print( nums );                         //** print array
      Arrays.sort( nums );                   //** sort array
      print( nums );                         //** print again
   }
   static void print( double[ ] a ) {
      System.out.println();
      for ( int i = 0; i < a.length; i++ )
         System.out.println( a[ i ] );
   }   
}
2
Contributors
3
Replies
16
Views
2 Years
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.