使用java将两个长度相同的数组混合到一个数组中

4szc88ey  于 2021-07-06  发布在  Java
关注(0)|答案(3)|浏览(464)

我试图组合两个长度相同的数组,并返回一个数组,而前面的数组被洗牌,但我无法返回它。我尝试了以下方法:

public class PerfectShuffle {
    public static int[]interleave (int[]a1,int[]a2) {
        int[] arrayBla1 = {1, 2, 3};
        int[] arrayBla2 = {4, 5, 6};
        for (int i = 0, j = 0; i < 6 && j < 3; i++, j++) {
            a2[i] = arrayBla1[j];
            i++;
            a2[i] = arrayBla2[j];
        }
        for (int n : a2);
        return a1;
    }
    public static void main (String[]args){
                int[] a1={1, 2, 3};
                int[] a2={4, 5, 6};
            System.out.println(interleave(a1,a2));
    }
}
ckocjqey

ckocjqey1#

我对代码进行了注解;希望你能理解。继续练习。:-)

import java.util.Arrays;

public class PerfectShuffle {
    public static int[] interleave (int[]a1,int[]a2) {
        // if a1 has more elements than a2 this function will fail; so return a save method coll
        if (a1.length > a2.length)
              return interleave(a2, a1);
        // result array need length combined of both entry arrays
        int[] res = new int[a1.length + a2.length];
        // need a counter j to handle position of result array
        // now loop throw one of both and add alternating elements from both arrays
        for (int i = 0, j = 0; i < a1.length; i++) {
            // add element i from a1 to result array on position j and add 1 to j
            res[j++] = a1[i]; 
            // add element i from a2 to result array on position j and add 1 to j
            res[j++] = a2[i];
        }
        // if a1 < a2: add hangover elements of a2 onto result array
        for (int i = a1.length; i < a2.length; i++) {
            res[a1.length+i] = a2[i];
        }
        return res;
    }

    public static void main (String[]args){
            int[] a1={1, 2, 3};
            int[] a2={4, 5, 6, 7};
            System.out.println(Arrays.toString(interleave(a1,a2)));
    }
}
tzdcorbm

tzdcorbm2#

public static int[] interleave (int[] a1, int[] a2) {
    int[] result = new int[a1.length * 2];
    int j = 0;
    for (int i = 0; i < a1.length; i++) {
        result[j++] = a1[i];
        result[j++] = a2[i];
    }
    return result;
}

public static void main(String[] args) {
    interleave(new int[]{1, 2, 3}, new int[]{4, 5, 6});
}

使用调试器逐步完成上述代码。返回的数组是:

[1, 4, 2, 5, 3, 6]

上面的代码是基于假设参数的方法 interleave() 是等长数组,否则上述代码将无法工作。

rqdpfwrv

rqdpfwrv3#

希望有帮助。

public static int[]interleave (int[]a1,int[]a2) {
    // you don't need this, because you are passing then in parameters
    //int[] arrayBla1 = {1, 2, 3};
    //int[] arrayBla2 = {4, 5, 6};

    // here you gonna have the final array, sized a1 plus a2.
    int[] result = new int[a1.length + a2.length];

    // here is how you control the index of the first two arrays, a1 and a2.
    int j = 0;
    int k = 0;

    // When you uses arrays, you have to take care of the length, because the index starts in 0.
    for (int i = 0; i < result.length - 1; i++) {

        //the result will have one item from each array.
        result[i] = a1[j];
        j++;

        //here is the trick, now you increment the result array to receive the data in the index position of the a2 array.
        i++;
        result[i] = a2[k];
        // And here you have to control the index to 'jump ahead' in the a2 array too. The same as we did above.
        k++;

    }

    return result;

}

public static void main (String[]args){
    int[] a1={1, 2, 3};
    int[] a2={4, 5, 6};

    int[] printArray = interleave(a1,a2);

    // You can not print the array using just 'System.out.println' or you will have the default toString() representation of an int array, not the data itself. So you have to iterate over the itens, passing the index and printing it. Now you will have the data.
    for(int i = 0; i < printArray.length; i++) {
        System.out.println(printArray[i]);
    }
}

相关问题