Java 我如何比较三个不同随机长度的数组(从1到5)中的每个对应索引,而不获得可能的越界异常

Java 我如何比较三个不同随机长度的数组(从1到5)中的每个对应索引,而不获得可能的越界异常,java,arrays,sorting,Java,Arrays,Sorting,我有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>=

我有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

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

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

无需创建额外阵列以均衡大小的更简单方法:

公共静态int[]getMaxValuesint[]x,int[]y,int[]z{ int size=Math.maxx.length,Math.maxy.length,z.length; int[]max=新的int[大小]; 对于int i=0;i int[]x={4,4,6};//随机数组长度为1-5,假设每个数组都包含这些值 int[]y={2,10}; int[]z={3,6,7,8,9}; System.out.PrintLnArray.toStringgetMaxValuesx,y,z; 输出:

[4, 10, 7, 8, 9]
更新 通过定义两个函数,可以使用流API创建以下实现,该流API将能够处理非硬编码数量的阵列:

私有静态int getAtIndexint[]arr,int i{ 返回iarr.length .mapi->getMaxarrs.get.MapToIntar->getAtIndexarr,i .托雷; } 测试:

int[]maxValues=getMaxValues->Stream.ofx,y,z;//阵列的供应流 System.out.PRINTLNARAYS.toStringmaxValues;
无需创建额外阵列以均衡大小的更简单方法:

公共静态int[]getMaxValuesint[]x,int[]y,int[]z{ int size=Math.maxx.length,Math.maxy.length,z.length; int[]max=新的int[大小]; 对于int i=0;i int[]x={4,4,6};//随机数组长度为1-5,假设每个数组都包含这些值 int[]y={2,10}; int[]z={3,6,7,8,9}; System.out.PrintLnArray.toStringgetMaxValuesx,y,z; 输出:

[4, 10, 7, 8, 9]
更新 通过定义两个函数,可以使用流API创建以下实现,该流API将能够处理非硬编码数量的阵列:

私有静态int getAtIndexint[]arr,int i{ 返回iarr.length .mapi->getMaxarrs.get.MapToIntar->getAtIndexarr,i .托雷; } 测试:

int[]maxValues=getMaxValues->Stream.ofx,y,z;//阵列的供应流 System.out.PRINTLNARAYS.toStringmaxValues;
我认为我们应该这样想

阵列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 我们需要的是每一列中的最大值


largestArr=5,5,32,6,23,43,56我认为我们应该这样想

阵列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 我们需要的是每一列中的最大值


largestArr=5,5,32,6,23,43,56请注意,未明确设置的值将默认为0。如果数组有负数,则需要首先用Integer.MIN_值填充数组注意,未显式设置的值将默认为0。如果数组有负数,则需要首先用Integer.MIN_值填充数组