繁体   English   中英

我在使用 Collections.sort 方法对数组列表进行排序时遇到问题 - 我不断收到错误消息

[英]I have a problem with sorting my arraylist using the method Collections.sort - I keep getting an error message

Note: EarthquakeTester.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

我在我的外壳上查找它然后我的消息说明了这一点

javac -Xlint EarthquakeTester.java
EarthquakeTester.java:80: warning: [unchecked] unchecked method invocation: method sort in class Collections is applied to given types
          Collections.sort(earthquakeArrayList);
                          ^
  required: List<T>
  found: ArrayList<Earthquake>
  where T is a type-variable:
    T extends Comparable<? super T> declared in method <T>sort(List<T>)

我不知道该怎么做。

这里的代码来自下面的测试类。

         ArrayList<Earthquake> earthquakeArrayList = new ArrayList<>(); 

if(mag < 3)
          earthquakeArrayList.add(new NonClassified(TimeAndDate, lat, lon, mag, id, place));

else
 earthquakeArrayList.add(new Classified(TimeAndDate, lat, lon, mag, id, place));
     
 Collections.sort(earthquakeArrayList);

这里的代码来自地震类

public abstract class Earthquake implements Comparable 
.....
...
...

      public int compareTo(Object obj)
 {
     Earthquake other = (Earthquake) obj;
     
     if(this.magnitude < other.getMagnitude() )
     {return 1;}
     else if(this.magnitude > other.getMagnitude())
             {return -1 ;}
     else 
         return 0;
 }
    

您需要将地震ArrayList 定义为参数化的ArrayList:

ArrayList<Earthquake> earthquakeArrayList = new ArrayList<>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM