Java Range查找最大和最小int之间的差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18992276/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:09:12  来源:igfitidea点击:

Java Range find difference between largest and smallest int

java

提问by user2268587

I'm having trouble figuring out how exactly to make it find the max number and minimum number in the array.

我无法弄清楚如何准确地找到数组中的最大数和最小数。

Write a method range that accepts an ArrayList of integers as a parameter and that returns the range of values contained in the list, which is defined as 1 more than the difference between the largest and smallest elements. For example if a variable called list stores the following values:

编写一个方法 range ,它接受一个整数的 ArrayList 作为参数,并返回列表中包含的值的范围,该范围被定义为比最大和最小元素之间的差大 1。例如,如果名为 list 的变量存储以下值:

[18, 14, 29, 12, 7, 25]

[18、14、29、12、7、25]

The call of range(list) should return 23, because this is one more than the largest difference between any pair of values (29 - 7 + 1 = 23). An empty list is defined to have a range of 0.

range(list) 的调用应该返回 23,因为这比任何一对值之间的最大差值 (29 - 7 + 1 = 23) 多一个。空列表定义为范围为 0。

So far I have this:

到目前为止,我有这个:

public static int range(ArrayList<Integer> list)
{
    int min = 0;
    int max = 0;
    int range = 0;
  for (int i: list)
    {
       if (list.size() > 0)
        {
         range = max - min + 1;
        }
    }
      return range;
}

Thank you VERY MUCH!

非常感谢!

采纳答案by Luca Mastrostefano

You have more than method to achive this goal.

你有更多的方法来实现这个目标。

Using Collections (more compact but expensive because it iterates two times on the list, one to find the max and one to find the min):

使用集合(更紧凑但更昂贵,因为它在列表上迭代两次,一次找到最大值,另一次找到最小值):

public static int range(final ArrayList<Integer> list) {
    if (list.isEmpty()) {
        return 0;
    } else {
        return (Collections.max(list) - Collections.min(list)) + 1;
    }
}

Or using your own algorithm like this (more code but finds min and max with just one loop):

或者像这样使用你自己的算法(更多的代码,但只需一个循环就可以找到最小值和最大值):

public static int range(final ArrayList<Integer> list) {
    if (list.isEmpty()) {
        return 0;
    } else {
        int max = list.get(0);
        int min = list.get(0);
        for (final int i : list) {
            if (i > max) {
                max = i;
            } else if (i < min) {
                min = i;
            }
        }
        return (max - min) + 1;
    }
}

回答by Alexis C.

You never calculate the max and the min value in your loop.

您永远不会计算循环中的最大值和最小值。

Hint : In this loop, find the max and the min value. Then calculate the range and return it.

提示:在这个循环中,找到最大值和最小值。然后计算范围并返回它。

int min = 0;
int max = 0;
for (int i: list){
 //find max and min here
}
return max - min + 1;

回答by Bohemian

This task only needs two lines:

这个任务只需要两行:

Collections.sort(list);
return list.isEmpty() ? 0 : list.get(list.size() - 1) - list.get(0);
  • Use the java JDK's API to do the heavy lifting for you
  • It's how you look at a problem that's important
  • Less code is good (as long as it's legible
  • 使用 java JDK 的 API 为您完成繁重的工作
  • 重要的是你如何看待问题
  • 代码越少越好(只要它清晰易读

回答by Oliver Atkinson

Why not use Collections.minand Collections.max

为什么不使用Collections.minCollections.max

int difference = Collections.max(list) - Collections.min(list);

回答by Damian Leszczyński - Vash

You could sort it and then peek fist and last item.

您可以对其进行排序,然后查看拳头和最后一项。

public static int range(List<Integer> input)
{
     if(input == null || input.size() == 0) throw new IllegalArgumentException("");

     if(input.size() == 1) return 0;

     List<Integer> copy = new ArrayList(input);

     Collections.sort(copy);

     int min = copy.get(0);
     int max = copy.get(copy.lenght-1);

     return max - min; 
}

This is not a perfect solution as list may contain nulls.

这不是一个完美的解决方案,因为列表可能包含空值。

You can start with simple comparison.

您可以从简单的比较开始。

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

for(Integer integer : input) {
   if(i == null) continue;

   int i = integer.intValue();

   if(i < min) {
     min = i;
   } 

   if(i > max) {
    max = i;
   } 
}

return max - min;

回答by javaNinja

public static int range(ArrayList<Integer> list){
    int min = list.get(0);
    int max = list.get(0);
    for (int i = 0; i < list.size(); i++){
        if (list.get(i) > max)
            max = list.get(i);
        if ((list.get(i) < min))
            min = list.get(i);
    }
    return max-min+1;
}