//aList.java -- by tsaiwn@csie.nctu.edu.tw // test array <---> Collection (Stack, Vector, List, ...) // test List in java.util.* import java.util.*; class aList { static java.io.PrintStream cout = System.out; public static void main(String xx[ ]) { Integer x[ ] = {55, 88, 38, 49, 58}; List a = new ArrayList(Arrays.asList(x)); cout.print("Original list a: "); print(a); // Note that in Java, Stack is a Vector, Vector is a List // use Collections.sort( ) to sort List, Vector, Stack // use Arrays.sort( ) to sort array of any data Collections.sort(a); cout.print(" Collections.sort(a): "); print(a); // You can use the Comparator Collections.reverseOrder( ) Collections.sort(a, Collections.reverseOrder()); cout.print("do Collections.sort(a, Collections.reverseOrder()); "); print(a); testAry(x); cout.println("=== after testAry( ):"); print(x); } static void print(List x) { Iterator i = x.iterator( ); for( ;i.hasNext( ); ) System.out.print(" " + i.next( ) ); cout.println( ); } static void testAry(Integer y[ ]) { System.out.println("=== testAry ==="); Vector v = new Vector(Arrays.asList(y)); v.add(0, 85); // head v.add(v.size( ), 66); // tail // though we can sort/print Vector ... but .. Object [ ] yy = v.toArray( ); // convert to an Array print(yy); Arrays.sort(yy); // yy is an Array print(yy); Arrays.sort(yy, Collections.reverseOrder()); print(yy); y = new Integer[v.size( )]; // note this will refer to other place v.copyInto(y); // not for array of primitive type System.out.print("In testAry, y== "); print(y); // please check the content on return } static void print(Object b[ ]) { // All elements in array b[ ] for(Object x: b) System.out.print(" " + x ); // for each System.out.println( ); } }//class /****************** D:\COURSE\OOP\ppnt>javac aList.java D:\COURSE\OOP\ppnt>java aList Original list a: 55 88 38 49 58 Collections.sort(a): 38 49 55 58 88 do Collections.sort(a, Collections.reverseOrder()); 88 58 55 49 38 === testAry === 85 55 88 38 49 58 66 38 49 55 58 66 85 88 88 85 66 58 55 49 38 In testAry, y== 85 55 88 38 49 58 66 === after testAry( ): 55 88 38 49 58 D:\COURSE\OOP\ppnt> ********************************/