leochiang

          BlogJava 聯系 聚合 管理
            21 Posts :: 0 Stories :: 1 Comments :: 0 Trackbacks
          package core_java;
          import java.util.Scanner;

          public class ExchangeArray {

              /**
               * 
          @param args
               
          */
              public static int[] split2array(String str){
                  String[] m = null;
                  m = str.split(",");
                  int[] a = new int[m.length];
                  for(int i=0;i<m.length;i++){
                      a[i] =  Integer.parseInt(m[i]);
                  }
                  return a;
              }
              
              public static int[] exchange(int[] a){
                  int[] b = new int[a.length];
                  for(int j=0;j<a.length;j++){
                      b[j]=a[a.length-1-j];
                  }
                  return b;
              }
              public static int[] bubsort(int[] a){//冒泡排序
                  int[] b = a;
                  for (int i = 0; i < b.length; i++) {
                      for(int j=i;j<b.length;j++){
                          int temp;
                          if(b[i]<b[j]){
                              temp = b[i];
                              b[i] = b[j];
                              b[j] = temp;
                          }
                      }
                      
                  }
                  return b;
              }
              
              public static int[] selsort(int[] a){//選擇排序
                  int[] b = a;
                  for (int i = 0; i < b.length; i++) {
                      int minIndex = i;//以最小值為基準
                      for(int j=i;j<b.length;j++){
                          int temp1 = b[minIndex];
                          int temp2 = b[j];
                          if(temp1>temp2){
                              minIndex = j;
                          }
                      }
                      if(minIndex!=i){
                          int temp;
                          temp = b[minIndex];
                          b[minIndex] = b[i];
                          b[i] = temp;
                      }
                  }
                  return b;
              }
              
              public static void print(int[] a){
                  for(int m=0;m<a.length-1;m++){
                      System.out.print(a[m]+",");
                  }
                  System.out.print(a[a.length-1]);
              }
              public static void main(String[] args) {
                  // TODO Auto-generated method stub
                  System.out.println("請輸入序列(以逗號隔開回車結束):");
                  Scanner sc = new Scanner(System.in);
                  String ss = sc.nextLine();
                  String[] o = ss.split(",");
                  System.out.println("原序列:");
                  for (int i = 0; i < o.length-1; i++) {
                      System.out.print(o[i]+",");
                  }
                  System.out.println(o[o.length-1]);
                  int[] r = exchange(split2array(ss));
                  System.out.println("處理后的序列:");
                  for(int m=0;m<r.length-1;m++){
                      System.out.print(r[m]+",");
                  }
                  System.out.println(r[r.length-1]);
                  System.out.println("選擇排序后的結果:");
                  print(selsort(r));
                  /*System.out.println();
                  System.out.println("冒泡排序后的結果:");
                  print(bubsort(r));
          */
              }

          }

          注意其中的異常:
          數組越界異常           ArrayIndexOutOfBoundsException
          空指針異常             NullPointerException






          posted on 2012-08-02 17:57 leoChiang 閱讀(237) 評論(0)  編輯  收藏 所屬分類: 技術文檔
          主站蜘蛛池模板: 松潘县| 漯河市| 永靖县| 古浪县| 荣昌县| 望都县| 玉环县| 大关县| 独山县| 兰坪| 伊宁县| 枣强县| 新和县| 迁安市| 峨边| 湄潭县| 雷州市| 凤阳县| 扎兰屯市| 那坡县| 长泰县| 石林| 修文县| 双城市| 宜章县| 盘山县| 凤凰县| 承德市| 洛宁县| 三河市| 金溪县| 江津市| 明星| 红安县| 石棉县| 江达县| 潼关县| 保亭| 南和县| 磴口县| 察隅县|