【问题标题】:How to find second highest number in ArrayList of Objects value [duplicate]如何在对象值的ArrayList中找到第二高的数字[重复]
【发布时间】:2021-06-18 05:33:40
【问题描述】:

这是我得到的最高价值,这似乎很有效,但我正在努力以类似的方式获得第二高的价值。

public Show GetHighestScore() {      
    Show highest = shows.get(0);
    for (Show show : shows) {
        if (show.getScore() > highest.getScore()) {
            highest = show;
        }
    }
    return highest;
}

【问题讨论】:

  • 能否把 Show 类的代码放上来,以便我们提出更好的解决方案
  • 如果您的 java 版本为 8 或更高,您可以使用流对 ArrayList 进行排序,之后您可以获得第 K 个最小元素在您的情况下为第二小元素: public Show kthSmallestUsingStream(List 显示, int k) { if (shows.size() sortedShows = shows.stream() .parallel() .sorted((sh01, sh02) -> sh01.getScore() - sh02.getScore()) .collect(Collectors.toList());返回 sortedShows.get(k - 1); }

标签: java arraylist


【解决方案1】:

这是查找数组中第 K 个最小/最大元素的典型问题。我建议你访问这个link。 但是在您的情况下,如果您愿意获得第二高的数字,您可以使用与选择排序或 Luis Ka 提供的方式类似的想法对数组进行两次传递。

【讨论】:

    【解决方案2】:
    public float secondHighest(ArrayList<Float> array){
      float highest = 0;
      for(float i : array){
          if(i > highest) highest = i;
      }
      float secondHighest = 0;
      for(float i : array){
          if(i > secondHighest && i < highest) secondHighest = i;
      }
      return secondHighest;
    }
    

    【讨论】:

    • 此代码无法编译,分配时出现 2 个编译错误从浮点到 int 的可能有损转换 此外,它对输入 array 中的值进行了假设,并将提供不正确的当元素少于 2 个时导致边缘情况。
    • 当然。这只是最简约的原则。只需一点点 Java 知识,任何人都可以根据自己的需要扩展代码。如果您不知道如何使函数适应您的代码,我可以为您提供帮助。
    【解决方案3】:

    如果数据中没有第二高的值并且不对输入数据中的值做任何假设,这应该可以一次性完成并返回适当的null

    static <T extends Comparable> T find2ndMax(List<T> data) {
        if (null == data || data.size() < 2) {
            return null;
        }
        T max1 = data.get(0).compareTo(data.get(1)) > 0 ? data.get(0) : data.get(1);
        T max2 = data.get(0).compareTo(data.get(1)) < 0 ? data.get(0) : data.get(1);
        
        for (int i = 2, n = data.size(); i < n; i++) {
            T x = data.get(i);
            if (x.compareTo(max1) > 0) {
                max2 = max1;
                max1 = x;
            } else if (x.compareTo(max2) > 0) {
                max2 = x;
            }
        }
        return max2;
    }
    

    测试(与接受答案的固定方法相比):

    List<List<Float>> tests = Arrays.asList(
        null,
        Collections.emptyList(),
        Arrays.asList(-1f),
        Arrays.asList(-1f, -2f),
        Arrays.asList(-1f, 1f),
        Arrays.asList(-1f, -2f, 3f),
        Arrays.asList(-1f, 4f, -2f, 2f)
    );
    for (List<Float> t : tests) {
        System.out.print("2nd max in " + t + ": ");
        System.out.print("find2Max() = " + find2ndMax(t) + "; secondHighest() = " );
        try {
            System.out.print(secondHighest(t)); 
        } catch(Exception ex) {
            System.out.print("FAIL: " + ex.getMessage());
        }
        System.out.println();
    }
    

    输出

    2nd max in null: find2Max() = null; secondHighest() = FAIL: null
    2nd max in []: find2Max() = null; secondHighest() = 0.0
    2nd max in [-1.0]: find2Max() = null; secondHighest() = 0.0
    2nd max in [-1.0, -2.0]: find2Max() = -2.0; secondHighest() = 0.0
    2nd max in [-1.0, 1.0]: find2Max() = -1.0; secondHighest() = 0.0
    2nd max in [-1.0, -2.0, 3.0]: find2Max() = -1.0; secondHighest() = 0.0
    2nd max in [-1.0, 4.0, -2.0, 2.0]: find2Max() = 2.0; secondHighest() = 2.0
    

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2018-09-22
      • 2011-12-20
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多