Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
            
                    
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        5年前关闭。
                    
                
        

我尝试用返回类型整数洗牌两个数组。例如,我有{1、2、3}和{0、5}。我得到的结果是{1,0}。因此,我不确定如何将其余数字打印在数组中。

这是我所拥有的:

public int[] shuffle(int[] A, int[] B) {
    int shuff = 0;
    int[] out = { A[shuff], B[shuff] };

    for (int i = 0; i < A.length; i++) {
        for (int j = 0; j < B.length; j++) {
            int shuffle = A[i];
            A[i] = B[j];
            B[j] = shuffle;
        }
    }
    return out;
}

最佳答案

带有:

int[] out = { A[shuff], B[shuff] };


您只需输入A的第一个数字和B的第一个数字,循环就没有关系了。然后您返回,因此它将始终返回{1,0}

如果您期望结果为:{1, 0, 2, 5, 3}我会这样写:

public static int[] interleave(int[] a, int[] b) {
    int[] out = new int[a.length + b.length];
    int j = 0;
    int maxLength = Math.max(a.length, b.length);
    for (int i = 0; i < maxLength; i++) {
        if (i < a.length) {
            out[j++] = a[i];
        }
        if (i < b.length) {
            out[j++] = b[i];
        }
    }
    return out;
}


运行代码段:
http://rextester.com/ISTE29382

10-06 12:41
查看更多