Comparable Interface
Objects that have an ordering are compared using the compareTo() method.
14. Integer Sorting Program
Answer:
No
Integer Sorting Program
static void sort( int[] )
method of Arrays
. The method is static
so it can be used without constructing 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 14:
Could compareTo()
be used to decide on the order for an array of String
references?