import java.util.Arrays; import java.util.Scanner; /** * Illustrates the use of an array and the Arrays class. * Students will extend this to use an ArrayList and the Collections class. * * @author David Mutchler. * Created Sep 21, 2008. */ public class Main { /** * Read integers from standard input * and print them in sorted order (smallest to largest). * Do this twice, once using an array and once using an ArrayList. * * @param args Command-line arguments (unused in this program) */ public static void main(String[] args) { Main.doItWithAnArray(); } /* * Read integers from standard input * and print them in sorted order (smallest to largest). */ private static void doItWithAnArray() { Scanner scanner = new Scanner(System.in); System.out.println("Enter positive integers, separated by whitespace."); System.out.println("Enter control-Z on a line by itself to terminate input."); int[] numbers = new int[10]; int k; for (k = 0; scanner.hasNextInt(); ++k) { numbers[k] = scanner.nextInt(); } int numberOfInputs = k; Arrays.sort(numbers, 0, numberOfInputs); System.out.println("The size of the array is " + numberOfInputs); System.out.println("The capacity of the array is " + numbers.length); System.out.print("Sorted, they are: "); for (int i = 0; i < numberOfInputs; i++) { System.out.print(numbers[i] + " "); } System.out.println(); } }