英文:
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
问题
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}; // 长度在1-5之间随机,假设每个数组包含这些值
int[] y = {2, 4};
int[] z = {5, 6, 7, 8, 9};
// 理想情况下,在某种算法之后,largest[] 应该包含 {5, 6, 7, 8, 9}
我最初考虑了一个 for 循环,但是由于数组的随机长度特性,我的循环最终会抛出索引越界异常,因为 x/y/z 不会在索引 [i] 处包含一个值。还有其他方法吗?
for (int i = 0; i < size; i++) {
if ((x[i] > y[i]) && (x[i] > z[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];
}
}
英文:
I have 3 arrays of random length. I want to create a new array that stores the largest value from comparing those 3 arrays at each index.
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};
// ideally after some sort of an algorithm largest[] should hold {5, 6, 7, 8, 9}
I initially thought of a for loop, but my loop will eventually throw me a out of bound exception, because of the random size length nature of the arrays and x/y/z won't hold a value at index [i]. Any other ways?
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];
}
}
答案1
得分: 1
有几种方法可以做到这一点。这里有一种方法,它避免了大量的条件语句,代价是更多的内存消耗。
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];
// 从 x 复制值到新数组
for(int i = 0; i < x.length; i++){
nooX[i] = x[i];
}
// ... 复制粘贴上述代码,并对 nooY 和 nooZ 数组执行相同操作
int[] largest = new int[size];
// ... 复制粘贴你的代码,使用 nooX、nooY 和 nooZ 替代 x、y 和 z
英文:
There are several ways of doing this. Here's one that avoids a ton of conditional statements at the cost of more memory.
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
答案2
得分: 1
一个更简单的方法,无需创建额外的数组来使大小相等:
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}; // 随机数组长度在1-5之间,假设每个数组包含这些值
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();
}
// 使用 Supplier 以便能够两次使用数组的流
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)); // 提供数组的流
System.out.println(Arrays.toString(maxValues));
英文:
A simpler approach without creating extra arrays to equalize size:
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;
}
Test:
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)));
Output:
[4, 10, 7, 8, 9]
Update<br/>
Defining a couple of functions allows to create the following implementation using Stream API that would be able to handle non-hardcoded number of arrays:
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();
}
Test:
int[] maxValues = getMaxValues(() -> Stream.of(x, y, z)); // supply stream of arrays
System.out.println(Arrays.toString(maxValues));
答案3
得分: 0
array1 = 1, 2, 3, 4, 6, 7
array2 = 3, 4, 5, 6, 23, 4
array3 = 5, 5, 32, 3, 2, 43, 56
Like a matrix
<pre>
1 2 3 4 6 7
3 4 5 6 23 4
5 5 32 3 2 43 56
</pre>
We need is the greatest value in every column.
largestArr = 5, 5, 32, 6, 23, 43, 56 <-- Like this
I hope this code is the answer to your problem.
public static int[] largestColumnsArr(int arr1[], int arr2[], int arr3[]) {
int[][] arr = {arr1, arr2, arr3};
int size = Math.max(arr3.length, Math.max(arr2.length, arr1.length));
int[] largestArr = new int[size];
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;
}
英文:
I think we should think this way
array1 = 1, 2, 3, 4, 6, 7
array2 = 3, 4, 5, 6, 23, 4
array3 = 5, 5, 32, 3, 2, 43, 56
Like a matrix
<pre>
1 2 3 4 6 7
3 4 5 6 23 4
5 5 32 3 2 43 56
</pre>
We need is the greatest value in every column.
largestArr = 5, 5, 32, 6, 23, 43, 56 <-- Like this
I hope this code is the answer to your problem.
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论