Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/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_Generics_Compiler Warnings - Fatal编程技术网

java未经检查的强制转换

java未经检查的强制转换,java,generics,compiler-warnings,Java,Generics,Compiler Warnings,我有一个Java的comparator类来比较地图条目: public class ScoreComp implements Comparator<Object> { public int compare(Object o1, Object o2) { Entry<Integer, Double> m1 = null; Entry<Integer, Double> m2 = null; try {

我有一个Java的comparator类来比较地图条目:

public class ScoreComp implements Comparator<Object> {

    public int compare(Object o1, Object o2) {

        Entry<Integer, Double> m1 = null;
        Entry<Integer, Double> m2 = null;

        try {
            m1 = (Map.Entry<Integer, Double>)o1;
            m2 = (Map.Entry<Integer, Double>)o2;
        } catch (ClassCastException ex){
            ex.printStackTrace();
        }

        Double x = m1.getValue();
        Double y = m2.getValue();
        if (x < y)
            return -1;
        else if (x == y)
            return 0;
        else
            return 1;        
     }

}
公共类ScoreComp实现了Comparator{
公共整数比较(对象o1、对象o2){
条目m1=空;
条目m2=空;
试一试{
m1=(映射条目)o1;
m2=(地图条目)o2;
}捕获(ClassCastException例外){
例如printStackTrace();
}
Double x=m1.getValue();
双y=m2.getValue();
if(x
当我编译这个程序时,我得到以下结果:

warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.Map.Entry<java.lang.Integer,java.lang.Double>
            m1 = (Map.Entry<Integer, Double>)o1;
警告:[未选中]未选中强制转换
找到:java.lang.Object
必需:java.util.Map.Entry
m1=(映射条目)o1;
我需要根据双值对映射条目进行排序

如果我创建了下面的比较器,那么在调用数组的sort函数时就会出现错误(我从映射中获取一个条目集,然后将该集合用作数组)

公共类ScoreComp实现了Comparator

如何实现此场景。

将重写为

public class ScoreComp implements Comparator<Map.Entry<Integer, Double>> {

    public int compare(Map.Entry<Integer, Double> o1, Map.Entry<Integer, Double> o2) {
        if ( o1.getValue()  < o2.getValue()  ) return -1;
        else if ( o1.getValue() == o2.getValue()  ) return 0;
        return 1;
    }
}
公共类ScoreComp实现了Comparator{
公共整数比较(Map.Entry o1,Map.Entry o2){
if(o1.getValue()
假设您正在使用此比较器对
树映射进行排序,那么这将不起作用<代码>树映射
比较器仅用于比较映射键,而不用于比较键->值项。如果您的比较器需要访问这些值,那么它必须在映射本身中查找它们,例如

final Map<Integer, Double> map = ....

public class ScoreComp implements Comparator<Integer>  {
   public int compare(Integer key1, Integer key2) {
    Double x = map.getValue();
    Double y = map.getValue();
    if (x < y)
        return -1;
    else if (x == y)
        return 0;
    else
        return 1; 
   }
}
最终地图=。。。。
公共类ScoreComp实现了Comparator{
公共整数比较(整数键1、整数键2){
Double x=map.getValue();
双y=map.getValue();
if(x

编辑:根据您的评论,我认为您最好的选择是创建一个封装ID和值的类,将这些值放入列表中,并对其进行排序

public class Item implements Comparable<Item> {
   int id;
   double value;

   public int compareTo(Item other) {
      return this.value - other.value;
   }
}
公共类项实现可比较{
int-id;
双重价值;
公共整数比较(项目其他){
返回this.value-other.value;
}
}
然后

List<Item> list = new ArrayList<Item>();
// ... add items here
Collections.sort(list);
List List=new ArrayList();
// ... 在此处添加项目
集合。排序(列表);

由于
本身是
可比的
,因此您不需要外部的
比较器
(除非您需要)。stacker已经描述了如何修复您显示的代码。下面是如何修复注释中的代码:首先,不要使用数组,因为数组不能与泛型一起使用(不能使用泛型类型的数组)。相反,您可以使用
列表
Collections.sort()
方法:

    List<Map.Entry<Integer, Double>> mList = 
        new ArrayList<Map.Entry<Integer, Double>>(Score.entrySet()); 
    Collections.sort(mList, new ScoreComp());
List mList=
新的ArrayList(Score.entrySet());
Collections.sort(mList,newscorecomp());

将集合用作数组是什么意思?请参阅stacker回复下面的注释这与异常无关;我已经相应地更改了标题和标签,即使您更新了问题,问题的链接也没有更改。。。嘿,url变了。那么这是如何工作的呢。编辑帖子标题后,问题的旧url将停止工作。然后我在调用排序时出错`Set mSet=Score.entrySet();对象[]arr=mSet.toArray();sort(arr,newscorecomp())`不,这不起作用,因为
TreeMap
例程不会传入
Map.Entry
值,无论您如何努力使其编译。我所问的所有代码都是在windows上使用6.5版netbeans编译的。但它不能在linux上的netbeans 6.8上编译。(java是1.6,两者都很奇怪)如果我需要根据值进行排序,并且值不是唯一的,因此键必须是整数,我希望将键和值保持在一起,以使代码看起来易于理解,那该怎么办?你真的在使用整数值做任何事情吗?谁不把double放进一个列表,然后排序呢?在我对double值排序之后,我需要能够识别哪个double值属于哪个id。唯一的问题是我找不到内置的pair数据结构。我多么希望我能接受两个正确的答案。你的回答很好,但对于我的工作,我会使用斯卡夫曼的方法。
    List<Map.Entry<Integer, Double>> mList = 
        new ArrayList<Map.Entry<Integer, Double>>(Score.entrySet()); 
    Collections.sort(mList, new ScoreComp());