package examples; /** * CS401 Examples 26T * * This handout is the same as ex26.java, but with the "new" generic typing * facilities added in Java 5.0. I have removed the comments from the previous * version and added comments to highlight the differences in the two. See also * class SortingT.java and class AbstractCD.java * * @author Dr. Ramirez */ public class ex26T { // We don't really need generic typing here, since we are not using // anything more specific than the Object class public static void showData(Object [] Ar, int perLine) { System.out.println("The data is: "); for (int i = 0; i < Ar.length-1; i++) { System.out.print(Ar[i] + ", "); if ((i+1) % perLine == 0) System.out.println(); } if (Ar.length > 0) System.out.println(Ar[Ar.length-1]); System.out.println(); } public static void main(String [] args) { String [] S = { "Talking Heads", "Midnight Oil", "Beatles", "Metallica", "U2", "Tori Amos", "Sarah McLachlan", "NIN", "Cranberries", "Garbage"}; Integer [] A = new Integer[23]; for (int i = 0; i < A.length; i++) A[i] = new Integer((i * 11)%(A.length)); showData(S, 5); System.out.println(); showData(A, 12); System.out.println(); // Calling the selectionSort method defined in the SortingT class // Compare this version to the Lewis / Loftus version SortingT.selectionSort(S); SortingT.selectionSort(A); showData(S, 5); System.out.println(); showData(A, 12); System.out.println(); /* * This code is the same as in the original ex26.java except for * the T in the name. However, look at AbstracCD.java to see the difference * in how the class is declared. */ MusicCD2 [] myCDs = new MusicCD2[4]; myCDs[0] = new MusicCD2("The Downward Spiral", "Nine Inch Nails", 8, "1/1/1994", new String[] {""}, 3901); myCDs[1] = new MusicCD2("Fables of the Reconstruction", "REM", 1, "", new String[] {""}, 2379); myCDs[2] = new MusicCD2("Diamond Life", "Sade", 4, "2/2/1985", new String[] {"Smooth Operator"}, 2692); myCDs[3] = new MusicCD2("Moby 18", "Moby", 0, "1/1/2002", new String[] {""}, 4274); showData(myCDs, 1); System.out.println(); SortingT.selectionSort(myCDs); showData(myCDs, 1); } }