如何获得数组的最小值,最大值?

这是我的代码。 我需要得到我的数组的最小值,最大值,以便能够得到范围,每当我输入数字时,最小值为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)); }}); 

 int[] convertedValues = new int[10]; int max = convertedValues[0]; for (int i = 1; i < convertedValues.length; i++) { if (convertedValues[i] > max) { max = convertedValues[i]; } } 

类似地,通过改变较小的符号来找到最小值。

使用Collections使用您的代码,您可以找到最小值和最大值。

以下是该示例代码:

  List 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); 

输出:

 2 100 
 int minIndex = list.indexOf(Collections.min(list)); 

要么

 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)); } } 

您可以对数组进行排序并获取位置0length-1

 Arrays.sort(convertedValues); int min = convertedValues[0]; int max = convertedValues[convertedValues.length - 1]; 

数组#sort(int []) :

将指定的整数数组按升序排序。

因此,排序后,第一个元素是最小元素,最后一个元素是最大元素。

 1. Sort the array. 2. Minimum is the First element. 3. Maximum is the last element of the array. 

仅供参考; 排序数组在计算上的优势。

我想你只是缺少一个检查..你应该将最小和最大变量定义为-1并添加以下检查。

 if (min == -1) { min = convertedValues[i]; } 
 for(int i = 1; i < convertedValues.length; i++) { if(convertedValues[i]>max) max=convertedValues[i]; if(convertedValues[i] 
 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 max) max = i; //or if(max == -1 || array[i] > array[max]) max = i; } return new int[] {min, max}; } 

排序至少需要O(n log(n)),n是数组中元素的数量。 如果只是查看数组中的每个元素,找到最小和最大元素是O(n)。 对于大型arrays,这要快得多。

  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); } 

在您的代码中删除以下部分:

 (min = max = convertedValues[0];) 

并将min初始化为1:

 (int min = 1)