【问题标题】:Shuffle two arrays with same length into one using JAVA使用 JAVA 将两个长度相同的数组混为一个
【发布时间】:2020-11-20 12:48:21
【问题描述】:

我正在尝试组合两个长度相同的数组并返回一个数组,其中之前的数组已打乱,但我无法返回它。我尝试过的如下:

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

【问题讨论】:

  • 你需要什么样的洗牌?您是否需要以特定的洗牌顺序生成的数组,比如首先来自数组 A 的 3 个元素,然后是来自 3 的元素(AAABBB)?还是每个数组中的 1 个项目,例如 ABABAB?
  • 嗨!是的!在我的情况下是:1,4,2,5,3,6。我真的很难理解编程背后的过程

标签: java arrays shuffle


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的解释和帮助!作为初学者,一步一步解释对理解有很大帮助! :)
  • 很高兴它有帮助。 :-)
【解决方案2】:
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()的参数是等长数组的假设,否则上面的代码将不起作用。

【讨论】:

  • 我运行它时没有得到任何结果:(
【解决方案3】:

希望对你有帮助。

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

【讨论】:

  • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
  • 非常感谢您的解释和帮助!作为初学者,一步一步解释对理解有很大帮助! :)
  • 这是最简单,甚至可能是最愚蠢的(以一种很好的方式哈哈哈)的解决方案,因为您可以看到索引以一种简单的方式逐步递增。索引有很多更好的解决方案,但是对于学习,这可能会有所帮助。
猜你喜欢
  • 2010-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多