返回列表 發帖

對陣列排序

運用 Arrays.sort() 函式, 對陣列排序.
  1. import java.util.Arrays;
  2. import java.io.Console;
  3. public class ch45
  4. {
  5.     public static void main(String args[])
  6.     {
  7.         int a[]=new int[10];   //宣告陣列並產生陣列實體
  8.         Console input=System.console();
  9.         System.out.println("請任意輸入10個整數");
  10.         for(int i=0; i<10; i++)
  11.         {
  12.             System.out.print("第"+(i+1)+"個數: ");
  13.             a[i]=Integer.parseInt(input.readLine());
  14.         }
  15.         Arrays.sort(a);    //對陣列排序
  16.         System.out.print("您剛剛輸入的10個數由小而大依序為: ");
  17.         for(int i=0; i<10; i++)
  18.         {
  19.             System.out.print(a[i]+" ");
  20.         }
  21.     }
  22. }
複製代碼

返回列表