日期:2014-05-20  浏览次数:20594 次

求助java合并数组
有2个升序数组int[] a={2,4,6,7,8,9};int[] b={4,7,8,23,25,46};将其合并为一个C数组,要求C为升序数组,并且使用上a和b数组,自己写算法!


------解决方案--------------------
for example
Java code
int[] a={2,4,6,7,8,9};
int[] b={4,7,8,23,25,46};
int[] tmp = new int[a.length + b.length]; //合并后的数组长度最长不会超过2个数组长度的和
int count = 0;
for (int i=0, j=0;i<a.length || j<b.length;) { //两个数组一起循环遍历
    if (i<a.length && j<b.length) {
        if (a[i] == b[i]) {
            tmp[count++] = a[i];
            i++;
            j++;
        } else if (a[i] < b[j]) {
            tmp[count++] = a[i];
            i++;
        } else {
            tmp[count++] = b[j];
            j++;
        }
    } else if (i<a.length) {
        tmp[count++] = a[i++];
    } else {
        tmp[count++] = b[j++];
    }
}
int[] result = new int[count]; //最终合并结果的数组长度为count
System.arraycopy(tmp, 0, result, 0, count); //拷贝最终结果
System.out.println(Arrays.toString(result)); //打印结果检验

------解决方案--------------------
Java code

/**
 * 有2个升序数组int[] a={2,4,6,7,8,9};int[] b={4,7,8,23,25,46};
 * 将其合并为一个C数组,要求C为升序数组,并且使用上a和b数组,自己写算法!
 */
public class Test {
    public static void main(String args[]){
        int[] a={2,4,6,7,8,9};
        int[] b={4,7,8,23,25,46};
        
        int[] result = getNewArray(a,b);
        for(int i = 0; i < result.length; i ++){
            System.out.println(result[i]);
        }
    }
    
    public static int[] getNewArray(int[] a, int[] b){
        int[] reustArray = new int[a.length + b.length];
        int count = 0;
        for(int i=0, j =0; (i < a.length || j < b.length);){
            if(i < a.length && j < b.length){
                int aTemp = a[i];
                int bTemp = b[j];
                
                if(aTemp > bTemp){
                    reustArray[count ++] = bTemp;
                    j ++;
                }else if(aTemp <= bTemp){
                    reustArray[count ++] = aTemp;
                    i++;
                }
            }else if(i < a.length){
                reustArray[count ++] = a[i];
                i ++;
            }else{
                reustArray[count ++] = b[j];
                j++;
            }
        }
        
        return reustArray;
    } 
}

------解决方案--------------------
归并排序。。。
Java code

    public static int[] mergeSort(int[] data1,int[] data2){  
            int[] temp=new int[data1.length+data2.length];  
            int i=0,j=0,iter=0;  
            for(;i<data1.length&&j<data2.length;){  
                if(data1[i]<=data2[j]){  
                    temp[iter]=data1[i];  
                    iter++;  
                    i++;  
                }  
                else{  
                    temp[iter]=data2[j];  
                    iter++;  
                    j++;  
                }  
            }  
            for(;i<data1.length;i++,iter++){  
                temp[iter]=data1[i];  
            }  
            for(;j<data2.length;j++,iter++){  
                temp[iter]=data2[j];  
            }  
            return temp;  
        }