未经检查的泛型的问题 [英] Issues with unchecked generics

查看:3436
本文介绍了未经检查的泛型的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个方法在编译时抛出以下警告:

I have a method that is throws the following warning when compiled:

warning: [unchecked] unchecked generic array creation for varargs parameter of     
type TableColumn<ObservableList,?>[]   tableview.getColumns().addAll(Col);

我知道如果方法是安全的,我可以添加@SafeVarargs注释来抑制警告。但是,我不确定我的方法是否安全。此外,当我添加注释(只是为了看看会发生什么)时,编译器给了我以下错误:

I know that if the method is safe, I can add the @SafeVarargs annotation to suppress the warning. However, I'm not certain that my method is safe. Also, when I added the annotation (just to see what might happen), the compiler gave me the following error:

Invalid SafeVarargs annotation. Method InsertSQL(TableView<ObservableList>) 
is not a varargs method.
public TableView InsertSQL(TableView<ObservableList> tableview) 

这是有问题的方法,完整:

Here is the method in question, in its entirety:

private TableColumn<ObservableList, String> Col; //declared outside the   
method at the beginning of the class.

@SafeVarargs
public TableView InsertSQL(TableView<ObservableList> tableview) {
    try {
        conn = DriverManager.getConnection(ConnCheck, user, password);
        SysList = FXCollections.observableArrayList();
        st = conn.prepareStatement(SysTableCall);
        SysData = st.executeQuery();

        for (int i = 0; i < SysData.getMetaData().getColumnCount(); i++) {
            final int j = i;
            Col = new TableColumn<>(SysData.getMetaData().getColumnName(i + 1));
            Col.setCellValueFactory(new CallbackImpl(j));
            //Col.setEditable(true);
            if (i == 0) {
                Col.setPrefWidth(344);
                Col.setText("Record Number");
            }
            if (i == 1) {
                Col.setPrefWidth(344);
                Col.setText("Description");
            }
            tableview.getColumns().addAll(Col); //warning found on this line
        }

        while (SysData.next()) {
            ObservableList<String> row = FXCollections.observableArrayList();
            for (int x = 1; x <= SysData.getMetaData().getColumnCount(); x++) {
                row.add(SysData.getString(x));
            }
            SysList.add(row);
        }
        tableview.setItems(SysList);
        st.execute();
        conn.close();
    } catch (SQLException ex) {
        try {
            PreparedStatement st1 = conn.prepareStatement(SysTableCheck);
            st.setString(1, RecCode);
            ResultSet tempData = st1.executeQuery();
            System.out.println(tempData.toString());
            //Logger.getLogger(SysInfo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex1) {
            Logger.getLogger(SysInfo.class.getName()).log(Level.SEVERE, null, ex1);
        }
    }
    return tableview;
}

我使用的是Java 8u40和Netbeans 8.0.2。理想情况下,我想处理警告(而不是压制它)。谢谢。

I'm using Java 8u40, and Netbeans 8.0.2. Ideally, I'd like to handle to warning (rather than suppress it). Thank you.

推荐答案

使用通用实例调用varargs方法会因阵列和泛型问题而导致出现这些警告。在您的情况下,只需使用 add 方法而不是 addAll ,只需一个参数。

Calling varargs methods with generic instances leads to these warnings due to problems with arrays and generics. In your case simply use the add method instead of addAll with one argument.

这篇关于未经检查的泛型的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆