Integer Sorting Program

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

No.

Integer Sorting Program

Here is a program that uses the static void sort( int[] ) method of Arrays. Since the method is static the program does not construct an Arrays object.

import java.util.Arrays;

class ArrayDemoOne
{
  public static void main ( String[] args )
  {
    int[] scramble = {148, -23, 67, 110, -17, 44, 103, -12, -8, 91, -12, 43, 0, 9, 80, 34, 21, 44, 15, 11};
    
    System.out.print("Scrambled array:  ");
    for ( int j=0; j < scramble.length; j++ )
      System.out.print( scramble[j] + " ");
      
    System.out.println();
    
    Arrays.sort( scramble );
    
    System.out.print("Sorted    array:  ");
    for ( int j=0; j < scramble.length; j++ )
      System.out.print( scramble[j] + " ");
      
    System.out.println();
       
  }
}

The output of the program is:

Scrambled array:  148 -23 67 110 -17 44 103 -12 -8 91 -12 43 0 9 80 34 21 44 15 11
Sorted    array:  -23 -17 -12 -12 -8 0 9 11 15 21 34 43 44 44 67 80 91 103 110 148

QUESTION 12:

Could compareTo() be used to decide on the order for an array of String references?