1 //aList.java -- by tsaiwn@csie.nctu.edu.tw 2 // test array <---> Collection (Stack, Vector, List, ...) 3 import java.util.*; 4 class aList { 5 public static void main(String xx[ ]) { 6 Integer x[ ] = {55, 88, 38, 49, 58}; 7 List a = new ArrayList(Arrays.asList(x)); 8 print(a); 9 Collections.sort(a); print(a); 10 Collections.sort(a, Collections.reverseOrder()); print(a); 11 testAry(x); 12 System.out.println("=== after testAry( ):"); 13 print(x); 14 } 15 static void print(List x) { 16 Iterator i = x.iterator( ); 17 for( ;i.hasNext( ); ) System.out.print(" " + i.next( ) ); 18 System.out.println( ); 19 } 20 static void testAry(Integer y[ ]) { 21 System.out.println("=== testAry ==="); 22 Vector v = new Vector(Arrays.asList(y)); 23 v.add(0, 85); // head 24 v.add(v.size( ), 66); // tail 25 // though we can sort/print Vector ... but .. 26 Object [ ] yy = v.toArray( ); // convert to an Array 27 print(yy); 28 Arrays.sort(yy); // yy is an Array 29 print(yy); 30 Arrays.sort(yy, Collections.reverseOrder()); 31 print(yy); 32 y = new Integer[v.size( )]; // note this will refer to other place 33 v.copyInto(y); // please check the content on return 34 System.out.print("In testAry, y== "); 35 print(y); 36 } 37 static void print(Object b[ ]) { 38 for(Object x: b) System.out.print(" " + x ); // for each 39 System.out.println( ); 40 } 41 }//class 42 /****************** 43 D:\COURSE\OOP\ppnt>javac aList.java 44 45 D:\COURSE\OOP\ppnt>java aList 46 55 88 38 49 58 47 38 49 55 58 88 48 88 58 55 49 38 49 === testAry === 50 85 55 88 38 49 58 66 51 38 49 55 58 66 85 88 52 88 85 66 58 55 49 38 53 In testAry, y== 85 55 88 38 49 58 66 54 === after testAry( ): 55 55 88 38 49 58 56 57 D:\COURSE\OOP\ppnt> 58 ********************************/