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

时间:2013-09-16 12:29:33

标签: java android

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

    }});

10 个答案:

答案 0 :(得分:25)

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

以下是该示例代码:

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

输出:

2
100

答案 1 :(得分:7)

int[] convertedValues = new int[10];
int max = convertedValues[0];

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

同样通过改变较小的符号来找到最小值。

答案 2 :(得分:6)

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

答案 3 :(得分:5)

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

Arrays.sort(convertedValues);

int min = convertedValues[0];
int max = convertedValues[convertedValues.length - 1];
  

Arrays#sort(int[])

     

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

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

答案 4 :(得分:3)

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

仅供参考; Advantages of Sorted Array on computation.

答案 5 :(得分:0)

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

if (min == -1) {
    min = convertedValues[i];
}

答案 6 :(得分:0)

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

答案 7 :(得分:0)

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

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

答案 8 :(得分:0)

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

答案 9 :(得分:0)

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

(min = max = convertedValues[0];)

并将min初始化为1:

(int min = 1)