Java 如何获得数组的最小值,最大值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18828091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the minimum,maximum value of an array?
提问by Dio
Here's my code. I need to get the minimum,maximum value of my array to be able for me get the range, whenever I input numbers the minimum value is 0. Please help me. Thank you:)
这是我的代码。我需要获取数组的最小值和最大值才能获取范围,每当我输入数字时,最小值为 0。请帮助我。谢谢:)
final AutoCompleteTextView inputValues = (AutoCompleteTextView) findViewById(R.id.txt_input);
final TextView txtMinimum = (TextView) findViewById(R.id.txtMinimum);
final TextView txtMaximum = (TextView) findViewById(R.id.txtMaximum);
final TextView txtRange = (TextView) findViewById(R.id.txtRange);
Button btncalculate = (Button)findViewById(R.id.btncalculate);
btncalculate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String []values = ( inputValues.getText().toString().split(","));
int[] convertedValues = new int[values.length];
// calculate for the minimum and maximum number
int min = 0;
int max=0;
min = max = convertedValues[0];
for (int i = 0; i < convertedValues.length; ++i) {
convertedValues[i] =Integer.parseInt(values[i]);
min = Math.min(min, convertedValues[i]);
max = Math.max(max, convertedValues[i]);
}
txtMinimum.setText(Integer.toString(min));
txtMaximum.setText(Integer.toString(max));
// calculate for the range
int range=max - min;
txtRange.setText(Integer.toString(range));
}});
采纳答案by Balaji Dhanasekar
int[] convertedValues = new int[10];
int max = convertedValues[0];
for (int i = 1; i < convertedValues.length; i++) {
if (convertedValues[i] > max) {
max = convertedValues[i];
}
}
Similarly find for the minimum value by changing lesser symbol.
同样通过更改较小的符号来查找最小值。
回答by dipali
int minIndex = list.indexOf(Collections.min(list));
or
或者
public class MinMaxValue {
public static void main(String[] args) {
char[] a = {'3', '5', '1', '4', '2'};
List b = Arrays.asList(ArrayUtils.toObject(a));
System.out.println(Collections.min(b));
System.out.println(Collections.max(b));
}
}
回答by BackSlash
You could sort the array and get the positions 0
and length-1
:
您可以对数组进行排序并获取位置0
和length-1
:
Arrays.sort(convertedValues);
int min = convertedValues[0];
int max = convertedValues[convertedValues.length - 1];
Sorts the specified array of ints into ascending numerical order.
将指定的整数数组按数字升序排序。
So, after sorting, the first element is the minimum, the last is the maximum.
所以,排序后,第一个元素最小,最后一个元素最大。
回答by JNL
1. Sort the array.
2. Minimum is the First element.
3. Maximum is the last element of the array.
Just FYI; Advantages of Sorted Array on computation.
仅供参考;排序数组在计算上的优势。
回答by Maxim Shoustin
Use Collections
with your code using it you can find minimum and maximum .
使用Collections
使用它,你可以找到最低和最高与您的代码。
following is the example code for that:
以下是示例代码:
List<Integer> list = Arrays.asList(100,2,3,4,5,6,7,67,2,32);
int min = Collections.min(list);
int max = Collections.max(list);
System.out.println(min);
System.out.println(max);
Output:
输出:
2
100
回答by user1401472
I think you are just missing one check .. You should define the min and max variables as -1 and add following check.
我认为您只是缺少一项检查。您应该将 min 和 max 变量定义为 -1 并添加以下检查。
if (min == -1) {
min = convertedValues[i];
}
回答by gifpif
for(int i = 1; i < convertedValues.length; i++) {
if(convertedValues[i]>max)
max=convertedValues[i];
if(convertedValues[i]<min)
min=convertedValues[i];
}
回答by brickBreaker
public static int[] getMinMax(int[] array) {
int min = Integer.MAX_VALUE; //or -1
int max = Integer.MIN_VALUE; //or -1
for(int i : array) { //if you need to know the index, use int (i=0;i<array.length;i++) instead
if(i < min) min = i; //or if(min == -1 || array[i] < array[min]) min = i;
if(i > max) max = i; //or if(max == -1 || array[i] > array[max]) max = i;
}
return new int[] {min, max};
}
Sorting takes at least O(n log(n)), n being the amount of elements in the array. If you simply look at every element in the array, finding the minimum and maximum element is in O(n). For big arrays, that's a lot faster.
排序至少需要 O(n log(n)),n 是数组中元素的数量。如果您简单地查看数组中的每个元素,则在 O(n) 中找到最小和最大元素。对于大数组,这要快得多。
回答by Philip Sheard
int n = Integer.parseInt(values[0]);
convertedValues[i] = n;
int min = n;
int max = n;
for(int i = 1; i < convertedValues.length; ++i) {
n = Integer.parseInt(values[i]);
convertedValues[i] = n;
min = Math.min(min, n);
max = Math.max(max, n);
}
回答by Macylnt
In your code remove the following part:
在您的代码中删除以下部分:
(min = max = convertedValues[0];)
and initialize min
to 1:
并初始化min
为 1:
(int min = 1)