使用 JAVA 将两个长度相同的数组打乱为一个

Shuffle two arrays with same length into one using JAVA

我正在尝试将两个长度相同的数组和 return 一个单独的数组组合在一起,并将之前的数组打乱顺序,但我无法 return 它。我尝试过的是以下内容:

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));
    }
}
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()的参数是等长数组,否则以上代码将无法运行。

我评论了代码;希望你能理解。继续练习。 :-)

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)));
    }
}

希望对您有所帮助。

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]);
    }
}