找到2个数组的最大长度

时间:2021-08-19 02:57:24

I am passing 2 arrays to the controller with different lengths, I want to execute a for loop and length of that will be the max of the length of 2 arrays. I am not getting how to execute that. I tried Math.max but its giving me error as cannot assign a value to the final variable length.

我将2个数组传递给控制器​​,长度不同,我想执行一个for循环,其长度将是2个数组长度的最大值。我没有得到如何执行。我尝试过Math.max,但它给了我错误,因为无法为最终变量长度赋值。

String[] x =0;
x.length = Math.max(y.length,z.length);
for(int i=0; i < x.length; i++)

The no of elements in x and y are not fixed. it changes what we are passing from the front end.

x和y中的元素的数量不固定。它改变了我们从前端传递的东西。

4 个解决方案

#1


Initialize the new array with the desired length:

使用所需长度初始化新数组:

String[] x = new String[Math.max(y.length,z.length)];

In case you don't need to create an array, just use the result of Math.max as conditional to stop your loop:

如果您不需要创建数组,只需使用Math.max的结果作为条件来停止循环:

for (int i = 0; i < Math.max(y.length,z.length); i++) {
    //...
}

#2


Just bring your Math.max() operation into the array's initialization.

只需将Math.max()操作带入数组的初始化。

String[] x = new String[Math.max(y.length, z.length)];

Here's an expansion for clarity:

为了清晰起见,这是一个扩展:

int xLength = Math.max(y.length, z.length);
String[] x = new String[xLength];

Edit: Unless, OP, you're not interested in creating another array...

编辑:除非,OP,你对创建另一个数组不感兴趣......

I want to execute a for loop and length of that will be the max of the length of 2 arrays

我想执行一个for循环,其长度将是2个数组长度的最大值

Just bring your Math.max() operation into your for loop:

只需将Math.max()操作带入for循环:

for(int i=0; i < Math.max(y.length, z.length); i++){
    //code here
}

#3


Set a variable to the maximum length of the arrays, create a new array with that length and then loop until that point.

将变量设置为数组的最大长度,创建具有该长度的新数组,然后循环直到该点。

int maxLen = Math.max(y.length, x.length);
String[] array = new String[maxLen];
for(int i = 0; i < maxLen; i++){
    // Loop code here
}

#4


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

for(int i=0; i < max_length ; i++){
 //...
}

you can use that max_length to create a new String[] if you are trying to create an array with total length of y and z arrays, like

如果你试图创建一个总长度为y和z数组的数组,你可以使用那个max_length来创建一个新的String []

String[] newArray = new String[max_length];

#1


Initialize the new array with the desired length:

使用所需长度初始化新数组:

String[] x = new String[Math.max(y.length,z.length)];

In case you don't need to create an array, just use the result of Math.max as conditional to stop your loop:

如果您不需要创建数组,只需使用Math.max的结果作为条件来停止循环:

for (int i = 0; i < Math.max(y.length,z.length); i++) {
    //...
}

#2


Just bring your Math.max() operation into the array's initialization.

只需将Math.max()操作带入数组的初始化。

String[] x = new String[Math.max(y.length, z.length)];

Here's an expansion for clarity:

为了清晰起见,这是一个扩展:

int xLength = Math.max(y.length, z.length);
String[] x = new String[xLength];

Edit: Unless, OP, you're not interested in creating another array...

编辑:除非,OP,你对创建另一个数组不感兴趣......

I want to execute a for loop and length of that will be the max of the length of 2 arrays

我想执行一个for循环,其长度将是2个数组长度的最大值

Just bring your Math.max() operation into your for loop:

只需将Math.max()操作带入for循环:

for(int i=0; i < Math.max(y.length, z.length); i++){
    //code here
}

#3


Set a variable to the maximum length of the arrays, create a new array with that length and then loop until that point.

将变量设置为数组的最大长度,创建具有该长度的新数组,然后循环直到该点。

int maxLen = Math.max(y.length, x.length);
String[] array = new String[maxLen];
for(int i = 0; i < maxLen; i++){
    // Loop code here
}

#4


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

for(int i=0; i < max_length ; i++){
 //...
}

you can use that max_length to create a new String[] if you are trying to create an array with total length of y and z arrays, like

如果你试图创建一个总长度为y和z数组的数组,你可以使用那个max_length来创建一个新的String []

String[] newArray = new String[max_length];