Java泛型问题,在编译时,提示警告: 需要: List<T> 找到: List<BookDetails>
天蓬老师
天蓬老师 2017-04-18 09:12:53
0
2
495

错误提示:
++++++++++
警告: [unchecked] 方法调用未经过检查: 将类 Collections中的方法 s
ort应用到给定的类型

Collections.sort(books);
                ^

需要: List<T>
找到: List<BookDetails>
其中, T是类型变量:

T扩展已在方法 <T>sort(List<T>)中声明的Comparable<? super T>

1 个警告
++++++++++

下面是源代码:

  public Collection<BookDetails> getBooks()throws Exception{
    Connection con=null;
    PreparedStatement prepStmt=null;
    ResultSet rs =null;
    List<BookDetails> books = new ArrayList<BookDetails>();
    try {
      con=getConnection();
      String selectStatement = "select * " + "from BOOKS";
      prepStmt = con.prepareStatement(selectStatement);
      rs = prepStmt.executeQuery();

      while (rs.next()) {

        BookDetails bd = new BookDetails(rs.getString(1), rs.getString(2), rs.getString(3),
           rs.getFloat(4), rs.getInt(5), rs.getString(6),rs.getInt(7));
        books.add(bd);
      }
    }finally{
      closeResultSet(rs);
      closePrepStmt(prepStmt);
      closeConnection(con);
    }
    
    Collections.sort(books);           // 出现警告的地方
    return books;
  }

问题: 这是什么原因?T不是只是一个类型符号吗?为什么必须要找到List<T>?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!