Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获得数组的最小值和最大值?_Java_Android - Fatal编程技术网

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

Java 如何获得数组的最小值和最大值?,java,android,Java,Android,这是我的密码。我需要获取数组的最小值和最大值,以便能够获取范围,每当我输入数字时,最小值为0。请帮帮我。谢谢:) final AutoCompleteTextView inputValues=(AutoCompleteTextView)findViewById(R.id.txt\u输入); 最终文本视图txtMinimum=(文本视图)findViewById(R.id.txtMinimum); 最终TextView txtMaximum=(TextView)findViewById(R.id.

这是我的密码。我需要获取数组的最小值和最大值,以便能够获取范围,每当我输入数字时,最小值为0。请帮帮我。谢谢:)

final AutoCompleteTextView inputValues=(AutoCompleteTextView)findViewById(R.id.txt\u输入);
最终文本视图txtMinimum=(文本视图)findViewById(R.id.txtMinimum);
最终TextView txtMaximum=(TextView)findViewById(R.id.txtMaximum);
最终文本视图txtRange=(文本视图)findViewById(R.id.txtRange);
按钮btncalculate=(按钮)findViewById(R.id.btncalculate);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图arg0){
字符串[]值=(inputValues.getText().toString().split(“,”);
int[]convertedValues=新的int[values.length];
//计算最小值和最大值
int min=0;
int max=0;
最小值=最大值=转换值[0];
对于(int i=0;i


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

Arrays.sort(convertedValues);

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

将指定的整数数组按升序进行排序

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

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

仅供参考

使用
集合
使用您的代码可以找到最小值和最大值

以下是用于此目的的示例代码:

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

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

if (min == -1) {
    min = convertedValues[i];
}
int[]convertedValues=newint[10];
int max=转换值[0];
对于(int i=1;i最大值){
最大值=转换值[i];
}
}
同样,通过更改较小的符号来查找最小值。

for(int i=1;ifor(int i = 1; i < convertedValues.length; i++) {
    if(convertedValues[i]>max) 
        max=convertedValues[i];
    if(convertedValues[i]<min)
        min=convertedValues[i]; 
}
if(转换值[i]>最大值) 最大值=转换值[i]; if(convertedValues[i]
公共静态int[]getMinMax(int[]array){
int min=Integer.MAX_VALUE;//或-1
int max=Integer.MIN_值;//或-1
对于(inti:array){//如果需要知道索引,请使用int(i=0;i max)max=i;//或者如果(max=-1 | | array[i]>array[max])max=i;
}
返回新的int[]{min,max};
}
排序至少需要O(n log(n)),n是数组中的元素数量。如果您只需查看数组中的每个元素,就可以找到O(n)中的最小和最大元素。对于大型数组,这要快得多。

int n=Integer.parseInt(值[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);
        }
转换值[i]=n; int min=n; int max=n; 对于(int i=1;i
在代码中删除以下部分:

(min = max = convertedValues[0];)
并将
min
初始化为1:

(int min = 1) 

或者,将它们定义为
Integer.parseInt(值[0])
。他将它们预设为
convertedValues[0]
,但还没有在其中存储任何内容,也没有解析出值或
值[0]
(就像循环中较低的两行那样)排序数组比扫描复杂度更高。对于小数组,这不是问题,但如果它们变得相当大,那么简单地迭代值并进行比较肯定会更快。同意DRobinson的意见,不应该进行排序,线性扫描更便宜。@dipali…很好,很好我知道这很愚蠢,但你可以从i=1开始:)关于此策略的一般说明:这意味着边界将是O(n log n)(请参阅:)。如果您想要到达O(n),则需要避免排序。使用Java 8流,您可以在一行中完成
int[] convertedValues = new int[10];
int max = convertedValues[0];

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