通用方法来查找3个值的中位数

编程入门 行业动态 更新时间:2024-10-28 07:28:34
本文介绍了通用方法来查找3个值的中位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要一种方法来获得3个值的中位数,我认为这是一个很好的机会来编写一个通用的方法,因为我实际上没有这样做。我写了这篇文章,看起来非常直截了当,虽然我得到了一个警告,但根据我的测试,它似乎工作正常。

我知道我可以使用固有排序的集合,或 Collections.sort(),但这种方法是为了理解。

我想查明几件事:

  • 我注意到这不起作用,如果我试图声明 medianHelper 与 Arrays.asList(a,b,c)为什么会这样?试图搜索这个结果给了我无关的结果,因为我不确定发生了什么,所以它是难以捉摸的。我得到了一个 UnsupportedOperationException ,但这不是我下面的方式。
  • 为什么我会收到警告?什么是错误/丢失?

    该方法如下:

    private static< T extends Comparable> T中位数(T a,T b,T c){ List< T> medianHelper = new ArrayList<>(); T max; T min; medianHelper.add(a); medianHelper.add(b); medianHelper.add(c); if(apareTo(b)> = 0){ max = a; min = b; } else { max = b; min = a; } if(maxpareTo(c)== -1){ max = c; } if(minpareTo(c)> = 0){ min = c; } medianHelper.remove(max); medianHelper.remove(min); 返回medianHelper.get(0);

    解决方案

    类型参数 T ,比如 Comparable 也是通用的。

    它应该是:

    private static< T extends Comparable <? super T>> T中位数(T a,T b,T c)

    此外,您可以排序> medianHelper 列表,因为它的元素将> Comparable 。因此,您的方法可以显着缩短为:

    private static< T extends Comparable< ;? super T>> T中位数(T a,T b,T c){ List< T> medianHelper = Arrays.asList(a,b,c); Collections.sort(medianHelper); return medianHelper.get(1); $ / code>

    请注意 Arrays.asList()返回一个不可修改的列表,这意味着您不允许在创建元素后添加/删除元素。如果你想自己进行比较,你可以使用新的ArrayList<> c $ c>来代替 Arrays.asList()然后手动将元素添加到它。

    I needed a method to get the median of 3 values, I thought it a good opportunity to write a generic method since I don't really have that practiced. I wrote this and it seems pretty straight-forward, though I get a warning, but it seems to work fine, according to my tests.

    I'm aware I could use an inherently sorted set, or Collections.sort(), but this approach is for the sake of understanding.

    I want to pinpoint a few things:

  • I noticed this doesn't work if I tried to declare medianHelper with Arrays.asList(a, b, c) why is this? Trying to search this gives me unrelated results and it's otherwise elusive since I'm not sure what is happening. I get an UnsupportedOperationException, but this is not present the way I have it below.
  • Why am I getting a warning? What is wrong/missing?
  • The method follows:

    private static <T extends Comparable> T median(T a, T b, T c) { List<T> medianHelper = new ArrayList<>(); T max; T min; medianHelper.add(a); medianHelper.add(b); medianHelper.add(c); if (apareTo(b) >= 0) { max = a; min = b; } else { max = b; min = a; } if (maxpareTo(c) == -1) { max = c; } if (minpareTo(c) >= 0) { min = c; } medianHelper.remove(max); medianHelper.remove(min); return medianHelper.get(0); }

    解决方案

    You haven't correctly introduced the type-parameter T, as Comparable is generic, too.

    It should rather be:

    private static <T extends Comparable<? super T>> T median(T a, T b, T c)

    Furthermore, you can just sort the medianHelper list, since its elements will be Comparable. So your method can be significantly shortened to:

    private static <T extends Comparable<? super T>> T median(T a, T b, T c) { List<T> medianHelper = Arrays.asList(a, b, c); Collections.sort(medianHelper); return medianHelper.get(1); }

    Note that Arrays.asList() returns an unmodifiable list, which means you're not allowed to add/remove elements after it's created. If you wish to do the comparisons yourself, you can use new ArrayList<> instead of Arrays.asList() and then manually add the elements to it.

    更多推荐

    通用方法来查找3个值的中位数

    本文发布于:2023-10-20 08:48:53,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:中位数   方法来

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!