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 ] );
}
}