Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
import java.io.*; import java.util.*; public class ComparableFractionTester { public static void main( String args[] ) throws Exception { Random generator = new Random( 17 ); // POPULATE AN ARRAYLIST OF FRACTIONS WITH RANDOM VALUES ArrayList AList = new ArrayList(); for (int i=5 ; i>=0 ; --i ) AList.add( new ComparableFraction(1+generator.nextInt(100), 1+generator.nextInt(100)) ); // numer and denom in [1..99] // MAKE DEEP COPY OF THOSE FRACTIONS IN THE ARAYLIST ABOVE, BUT STORE INTO PLAIN ARRAY OF FRACTION ComparableFraction[] plainArr = new ComparableFraction[ AList.size() ]; for ( int i = 0 ; i < plainArr.length ; ++i ) plainArr[i] = new ComparableFraction( AList.get(i) ); // PRINT BOTH CONTAINERS TO VERIFY THEY MATCH System.out.println("\nArrayList OF FRACTIONS UNSORTED:"); for ( ComparableFraction f : AList ) System.out.println( f ); System.out.println("\nplainArr OF FRACTIONS UNSORTED:"); for ( ComparableFraction f : plainArr ) System.out.println( f ); // SORT BOTH CONTAINERS USING APPROPRIATE LIBRARY Collections.sort( AList ); Arrays.sort( plainArr ); //RE-PRINT BOTH AND THEY SHOULD BE IDENTICAL AND IN SORTED ORDER System.out.println("\nArrayList OF FRACTIONS SORTED:"); for ( ComparableFraction f : AList ) System.out.println( f ); System.out.println("\nplainArr OF FRACTIONS SORTED:"); for ( ComparableFraction f : plainArr ) System.out.println( f ); }// END MAIN } // END