1. 程式人生 > >java 字串的比較與合併操作

java 字串的比較與合併操作

1.字元的排序 2 種方法

  1. public class Sort  
  2. {  
  3.     public static void main(String[] args)  
  4.     {  
  5.         int[] num1 = {4
    ,3,2,-2,-8,55,33,24,56,23,65,88,457,-23,-66};  
  6.         sort(num1);  
  7.     }  
  8.   
  9.     public static void sort(int[] num)  
  10.     {  
  11.         int j=0;  
  12.         while(j<num.length)  
  13.         {  
  14.             for
    (int i=0;i<num.length-1;i++)  
  15.             {  
  16.                 if(num[i]>num[i+1])  
  17.                 {  
  18.                     int temp;  
  19.                     temp = num[i];  
  20.                     num[i]=num[i+1];  
  21.                     num[i+1]=temp;  
  22.                 }  
  23.             }  
  24.             j++;  
  25.         }  
  26.         for(int i=0;i<num.length;i++)  
  27.         {  
  28.             System.out.print(num[i]+"   ");  
  29.         }  
  30.     }  

方法2

// 呼叫util的Arrays介面

  1. import java.util.Arrays;  
  2. public class Sort2  
  3. {  
  4.     public static void main(String[] args)  
  5.     {  
  6.         int[] num = {4,3,2,-2,-8,55,33,24,56,23,65,88,457,-23,-66};  
  7.         Arrays.sort(num);  
  8.         for(int i : num)  
  9.         {  
  10.             System.out.print(i+ " ");  
  11.         }  
  12.           
  13.         System.out.println();  
  14.         String[] str = {"Mike","Jerry","Tom","Lucy","Lily","Auther","Yoyo"};  
  15.         Arrays.sort(str);  
  16.         for(String s : str)  
  17.         {  
  18.             System.out.print(s + " ");  
  19.         }  
  20.     }  
  21.   
  22.       
  23. }  

  24. 陣列的合併
  25. 三種字元數組合並的方法

    public static String[] getOneArray() {
      String[] a = { "0", "1", "2" };
      String[] b = { "0", "1", "2" };

      String[] c = new String[a.length + b.length];

      for (int j = 0; j < a.length; ++j) {
       c[j] = a[j];
      }

      for (int j = 0; j < b.length; ++j) {
       c[a.length + j] = b[j];
      }

      return c;
     }//直接新建陣列重新賦值

     public static Object[] getTwoArray() {
      String[] a = { "0", "1", "2" };
      String[] b = { "0", "1", "2" };

      List aL = Arrays.asList(a);
      List bL = Arrays.asList(b);

      List resultList = new ArrayList();
      resultList.addAll(aL);
      resultList.addAll(bL);

      Object[] result = resultList.toArray();
      return result;
     }//呼叫ArrayLIst中轉,新增陣列到List

     public static String[] getThreeArray() {
      String[] a = { "0", "1", "2", "3" };
      String[] b = { "4", "5", "6", "7", "8" };
      String[] c = new String[a.length + b.length];
      System.arraycopy(a, 0, c, 0, a.length);
      System.arraycopy(b, 0, c, a.length, b.length);
      return c;
     }//System.arraycopy 方法將陣列放到新建的組合陣列中

     

    1.兩個字元數組合並的問題

    public String[] getMergeArray(String[] al,String[] bl) {
      String[] a = al;
      String[] b = bl;
      String[] c = new String[a.length + b.length];
      System.arraycopy(a, 0, c, 0, a.length);
      System.arraycopy(b, 0, c, a.length, b.length);
      return c;
     }

     

    2.字元陣列和整形數組合並問題

     public int[] getIntArray(int[] al,String[] bl) {
      int[] a = al;
      String[] b = bl;
      
      int[] ia=new int[b.length];
      for(int i=0;i<b.length;i++){
         ia[i]=Integer.parseInt(b[i]);
     }

      int[] c = new int[a.length + ia.length];
      System.arraycopy(a, 0, c, 0, a.length);
      System.arraycopy(ia, 0, c, a.length, ia.length);
      return c;
     }//數字字元與數字的組合