JavaFX 编译警告 - "uses unchecked or unsafe operations" - 原始数据类型?

JavaFX Compilation Warning - "uses unchecked or unsafe operations" - Raw Data Types?

我正在学习 JavaFX,特别是尝试使用 TableColumn 和 TableView classes 实现 table。我的程序编译没有错误,但我在编译时收到了一个不祥的警告:

C:\JavaFX_Sandbox>javac robotApp.java
Note: robotApp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

C:\JavaFX_Sandbox>

这足以让我感到奇怪,以至于我没有勇气实际尝试 运行 我的程序。我在 Whosebug 和许多其他站点上进行了一些挖掘,发现出现此警告的最可能原因是我使用的是原始数据类型。但如果是这样,我无法发现它们。如果不是,我担心这条消息可能意味着什么。

尽快描述我的程序:我创建了一个书呆子 robot.java class:

public class robot{

    private String name;
    private int intelligence;

    public robot(String a, int b){
        this.name = a;
        this.intelligence = b;
    }
}

没什么特别的。为了存储所有这些机器人,我创建了一个 robotMgr.java class,本质上是一个美化的 ArrayList 来容纳它们:

import java.util.ArrayList;
import java.util.List;
import javafx.collections.*;

public class robotMgr{

    private ArrayList<robot> robotList;
    private ObservableList<robot> oRobotList;

    robotMgr(){
        robotList = new ArrayList<robot>();
        robotList.add(new robot("R2-D2", 7));
        robotList.add(new robot("Cylon", 3));
        robotList.add(new robot("The Terminator", 5));
        robotList.add(new robot("WALL-E", 6));
        populateOList();
    }
    public void populateOList(){
        for(int i=0; i<robotList.size(); i++)
            oRobotList.add(robotList.get(i));
    }

    public List<robot> getRobotList(){
        return robotList;
    }
    public ObservableList<robot> getORobotList(){
        return oRobotList;
    }
}

(我在 ArrayList 中进行了更多的数据操作,但是 ObservableList 是必需的,因为 TableColumn class 期望从 ObservableList 中获取 table 元素。)

现在是主赛事:一个 JavaFX 程序,它获取上面的数据并将其显示在一个简单的两列中 table:

import javafx.application.*;
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.collections.*;

public class robotApp extends Application{

    public static void main(String[] args){
        launch(args);   
    }

    TableColumn<robot, String> colName;
    TableColumn<robot, Integer> colIntelligenge;
    TableView<robot> robotTable;

    BorderPane borderPane;
    Scene scene;
    Stage stage;

   @Override public void start(Stage primaryStage){

        robotMgr myBots = new robotMgr();

        colName = new TableColumn<robot, String>("Name");
        colName.setMinWidth(100);
        colName.setCellValueFactory(new PropertyValueFactory<robot, String>("Name"));
        colIntelligenge = new TableColumn<robot, Integer>("Intelligence");
        colIntelligenge.setMinWidth(100);
        colIntelligenge.setCellValueFactory(new PropertyValueFactory<robot, Integer>("Intelligence"));

        robotTable = new TableView<robot>();
        robotTable.getColumns().addAll(colName, colIntelligenge);
        robotTable.setItems(myBots.getORobotList());

        borderPane = new BorderPane();
        borderPane.setCenter(robotTable);
        scene = new Scene(borderPane, 600, 600);
        stage = primaryStage;
        stage.setScene(scene);
        stage.setTitle("Robot App");
        stage.show();

    }
}

我觉得一切都很好。一切编译正常,除了我上面提到的警告消息。奇怪的是,如果我只注释掉 "robotTable.getColumns().addAll(colName, colIntelligenge);" 行,消息就会消失。

所以...我完全莫名其妙。如果错误消息警告我使用原始数据类型,我不知道它们在我的程序中的什么位置。如果错误消息是关于其他事情的警告,我不知道那可能是什么。另外:为什么 addAll() 方法会成为导致警告的行?我想不出那对我来说是终生的。

问题是机器人的实例变量都是私有的吗?如果是这样,它不会在编译时产生特定的语法错误吗?或者是 ObservableList 的问题?明白 OL 是接口,而不是数据结构......但我不明白这怎么会把我绊倒在这里。

如果有人能提供一些帮助或建议,我将不胜感激。

非常感谢! -饶

您的代码对 运行 来说是完全安全的(它有错误,当您 运行 它时您会发现这些错误,但这些错误与您所询问的类型安全无关)。

基本上,问题是您正在调用的 addAll() 方法采用类型为通用类型的 "varargs" 参数。对于 TableView<S>TableView.getColumns() returns 和 ObservableList<TableColumn<S,?>> 以及您随后调用的 ObservableList<E> addAll() 方法采用可变参数:addAll(E... elements).所以你最终调用了一个带有签名 addAll(TableColumn<Robot, ?>... columns).

的方法

Varargs 是通过将它们转换为数组来处理的,因此当您调用 addAll 方法时,您隐式创建了一个 TableColumn<Robot, ?>[] 类型的数组。因为数组和参数化类型不能很好地协同工作,所以编译器无法确保此数组的类型安全。因此,它会发出警告。看,例如here 编译器为何无法在此处确保类型安全的示例。

addAll() 方法的作者可能应该对其进行编码以确保类型安全并对其进行注释以避免此警告(如果可能的话:我需要考虑更多比我现在的时间还多)。

要避免警告,您可以简单地调用 add 方法两次:

robotTable.getColumns().add(colName);
robotTable.getColumns().add(colIntelligenge);

或者,您可以创建 List 个 table 列,并将其传递给采用集合的(其他)addAll(...) 方法,而不是可变参数:

robotTable.getColumns().addAll(Arrays.asList(colName, colIntelligence));