繁体   English   中英

如何比较来自 1-5 的不同随机长度的三个数组中的每个对应索引,而不会出现可能的越界异常

[英]How do I compare each corresponding index from three array of different random lengths from 1-5 without getting a possible out of bound exception

我有 3 个随机长度的数组。 我想创建一个新数组,该数组存储在每个索引处比较这 3 个数组的最大值。

 int size1=x.length;
        int size2=y.length;
        int size3=z.length;
        int size=0;
        if (size1>=size2 && size1>=size3)
            size=size1;
        else if (size2>=size1 &&size2>=size3) {
            size=size2;
        }
        else if (size3>=size1 && size3>=size2) {
            size=size3;
        }
    int[] largest= new int[size];
    int[] x= {1, 4, 6};  // random array length from 1-5 and hypothetically each array hold these values
    int[] y= {2, 4};
    int[] z= {5, 6, 7, 8, 9};
    

// 理想情况下,在某种算法之后,最大 [] 应该保持 {5, 6, 7, 8, 9} 我最初想到的是 for 循环,但我的循环最终会抛出一个越界异常,因为随机数组的大小长度性质和 x/y/z 不会在索引 [i] 处保存值。 还有其他方法吗?

for (int i=0;i<size;i++) {
            if (x[i]>y[i]) && t1[i]>t3[i]) {
                largest[i]=x[i];
            }
            else if (y[i]>x[i]) && y[i]>z[i]) {
                largest[i]=y[i];
            }
            else if (z[i]>x[i]) && z[i]>y[i]) {
                largest[i]=z[i];
            }
        }

有几种方法可以做到这一点。 这是一个以更多内存为代价避免大量条件语句的语句。

int size = Math.max(x.length, Math.max(y.length, z.length));

int[] nooX = new int[size];
int[] nooY = new int[size];
int[] nooZ = new int[size];

// Copy over the values from x to the new array
for(int i = 0; i < x.length; i++){
    nooX[i] = x[i];
}

// ... Copy paste the above and do the same for arrays nooY and nooZ

int[] largest = new int[size];
// ... Copy paste your code, using nooX, nooY, and nooZ instead of x, y, and z

一种更简单的方法,无需创建额外的数组来均衡大小:

public static int[] getMaxValues(int[] x, int[] y, int[] z) {
    int size = Math.max(x.length, Math.max(y.length, z.length));

    int[] max = new int[size];

    for (int i = 0; i < size; i++) {
        int xi = i < x.length ? x[i] : Integer.MIN_VALUE;
        int yi = i < y.length ? y[i] : Integer.MIN_VALUE;
        int zi = i < z.length ? z[i] : Integer.MIN_VALUE;

        max[i] = Math.max(xi, Math.max(yi, zi));
    }

    return max;
}

测试:

int[] x= {4, 4, 6};  // random array length from 1-5 and hypothetically each array hold these values
int[] y= {2, 10};
int[] z= {3, 6, 7, 8, 9};

System.out.println(Arrays.toString(getMaxValues(x, y, z)));

输出:

[4, 10, 7, 8, 9]

更新
定义几个函数允许使用 Stream API 创建以下实现,该实现将能够处理非硬编码的数组数量:

private static int getAtIndex(int[] arr, int i) {
    return i < arr.length ? arr[i] : Integer.MIN_VALUE;
}

private static int getMax(IntStream values) {
    return values.max().getAsInt();
}

// use Supplier to be able to use stream of the arrays twice 
public static int[] getMaxValues(Supplier<Stream<int[]>> arrs) {
    return IntStream.range(0, getMax(arrs.get().mapToInt(arr -> arr.length)))
                    .map(i -> getMax(arrs.get().mapToInt(arr -> getAtIndex(arr, i))))
                    .toArray();
}

测试:

int[] maxValues = getMaxValues(() -> Stream.of(x, y, z)); // supply stream of arrays
System.out.println(Arrays.toString(maxValues));

我想我们应该这样想

数组 1 = 1, 2, 3, 4, 6, 7

数组 2 = 3, 4, 5, 6, 23, 4

数组 3 = 5, 5, 32, 3, 2, 43, 56

像矩阵

1  2  3  4  6  7

3  4  5  6  23 4

5  5  32 3  2  43 56

我们需要的是每一列中的最大值。

maximumArr = 5, 5, 32, 6, 23, 43, 56 <-- 像这样

我希望这段代码可以解决您的问题。

public static int[] largestColumnsArr(int arr1[], int arr2[], int arr3[]) {
    int[][] arr = {arr1, arr2, arr3};

    //The size of the largest sized array
    int size = Math.max(arr3.length, Math.max(arr2.length, arr1.length));
    int[] largestArr = new int[size];
    /*
     Takes the largest value in each column and assigns it to the array
      If it is try catch, if the size of the arrays is exceeded, the program exit is blocked.
     */
    for (int i = 0; i < size; i++) {
        int largestColumnValue = 0;
        try {
            for (int j = 0; j < arr.length; j++) {
                if (largestColumnValue < arr[j][i]) {
                    largestColumnValue = arr[j][i];
                }
            }
        } catch (Exception e) {
        }
        largestArr[i] = largestColumnValue;
    }
    return largestArr;
}

暂无
暂无

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

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