所以我想更改此设置以显示最大数字,而不是该数字所在的列表中的数字。所以基本上我希望它找出该数字的位置,然后打印出该数字是什么。
import java.util.*;
public class TwoArrays
{
public static void main (String args [] )
{
Random r = new Random();
int rangeMin = 0;
int rangeMax = 50;
ArrayList<Double> arrayList1 = new ArrayList<Double>();
ArrayList<Double> arrayList2 = new ArrayList<Double>();
for (int i =0;i<5;i++)
{
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
arrayList1.add(randomValue);
}
for (int i =0;i<5;i++)
{
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
arrayList2.add(randomValue );
}
Double maxInArray1 = Collections.max(arrayList1);
Double maxInArray2 = Collections.max(arrayList2);
if (maxInArray1>maxInArray2)
{
System.out.println("first array have max");
}
else if(maxInArray1<maxInArray2)
{
System.out.println("second array have max");
}
else
{
System.out.println("the max of second and first array is identical");
}
}
}
最佳答案
尝试添加:
Double maxOfBoth = Math.max(maxInArray1, maxInArray2);
这将为您提供 arrayList1
和 arrayList2
之间的最大数字
关于java - 使用数组查找最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32830779/