两个数组的合并

//合并两个数组
public class Merge {
  public static  int[] Merge (int[]a,int[] b){
     int  la=a.length;
     int lb=b.length;
      int pa=0,pb=0,pc=0;
      int[] c=new int[la+lb];
      
      while(pa<la&&pb<lb){
          if(a[pa]<b[pb]){
          c[pc++]=a[pa++];
         
          }
          else{
              c[pc++]=b[pb++];
          }
      }
     if(pa<la){
         while(pa<la){
             c[pc++]=a[pa++];
         }
     }
     else{
         while(pb<lb){
             c[pc++]=b[pb++];
         }
     }
     
     return c;
  }
  public static void main(String[] args){
      int[] a={1,3,5,7,9};
      int[] b={2,4,6,8};
    
      Merge merge=new Merge();
     //int[] c= merge.merge(a, b);
    int[] c= Merge.Merge(a,b);
    for(int i=0;i<c.length;i++){
        System.out.print(c[i]+" ");
    }
      
      
  }
}

 

posted @ 2012-06-18 21:06  ilxx1988  阅读(189)  评论(0编辑  收藏  举报