繁体   English   中英

使用 JAVA 将两个长度相同的数组混洗为一个

[英]Shuffle two arrays with same length into one using JAVA

我试图将两个长度相同的数组组合起来,并返回一个前一个数组打乱的单个数组,但我无法返回它。 我尝试过的是以下内容:

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

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

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) {
    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()的参数是等长数组的假设,否则上面的代码将不起作用。

希望能帮助到你。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM