【问题标题】:how to get the minimum,maximum value of an array? [duplicate]如何获取数组的最小值,最大值? [复制]
【发布时间】:2013-09-20 14:34:32
【问题描述】:

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

    }});

【问题讨论】:

  • 使用Java 8 stream,一行就能搞定

标签: java android


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    int[] convertedValues = new int[10];
    int max = convertedValues[0];
    
    for (int i = 1; i < convertedValues.length; i++) {
        if (convertedValues[i] > max) {
            max = convertedValues[i];
        }
    }
    

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

    【讨论】:

    • 我知道这很愚蠢,但你可以从 i=1 开始 :)
    【解决方案3】:
    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));
       }
    }
    

    【讨论】:

    • @dipali...好又好
    【解决方案4】:

    你可以对数组进行排序,得到0length-1的位置:

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

    Arrays#sort(int[]):

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

    所以,排序后,第一个元素是最小值,最后一个是最大值。

    【讨论】:

    • 排序数组比扫描复杂。对于小型数组,这不是问题,但如果它们变得非常大,那么简单地迭代值并进行比较肯定会更快。
    • 同意DRobinson,不应该去排序,线性扫描更便宜。
    【解决方案5】:
    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.

    【讨论】:

    【解决方案6】:

    我认为您只是缺少一项检查..您应该将 min 和 max 变量定义为 -1 并添加以下检查。

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

    【讨论】:

    • 或者,将它们定义为Integer.parseInt(values[0])。他将它们预设为convertedValues[0],但还没有在其中存储任何内容,也没有解析出值或values[0](就像在循环中低两行所做的那样)
    【解决方案7】:
    for(int i = 1; i < convertedValues.length; i++) {
        if(convertedValues[i]>max) 
            max=convertedValues[i];
        if(convertedValues[i]<min)
            min=convertedValues[i]; 
    }
    

    【讨论】:

      【解决方案8】:
      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)。对于大型数组,这要快得多。

      【讨论】:

        【解决方案9】:
                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);
                }
        

        【讨论】:

          【解决方案10】:

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

          (min = max = convertedValues[0];)
          

          并将min初始化为1:

          (int min = 1) 
          

          【讨论】:

            猜你喜欢
            • 2021-02-14
            • 1970-01-01
            • 2023-03-18
            • 2017-05-24
            • 2011-09-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-11-23
            相关资源
            最近更新 更多