[英]How to remove "unchecked generics array creation for varargs" in XYChart on JavaFX?
I am studying JavaFX and I am also a newbie in Java.我正在学习 JavaFX,我也是 Java 的新手。 (I am using Java8 in Windows.)
(我在 Windows 中使用 Java8。)
Sample code is like below.示例代码如下。
XYChart.Series series1 = new XYChart.Series();
eries1.setName("남자");
series1.setData(FXCollections.observableArrayList(
new XYChart.Data("2015", 70),
new XYChart.Data("2016", 40),
new XYChart.Data("2017", 50),
new XYChart.Data("2018", 30)
));
In this code, I can see a warning.在这段代码中,我可以看到一个警告。 That is,
那是,
Unchecked call to 'Data(X, Y)' as a member of raw type 'javafx.scene.chart.XYChart.Data'
Though the code works, I want to remove this warning because I am weak at Java generics programming and want to learn more about it through sample code.虽然代码有效,但我想删除这个警告,因为我在 Java 泛型编程方面很弱,想通过示例代码了解更多信息。
What is the right way of removing that warning?消除该警告的正确方法是什么?
Thank you.谢谢你。
Change the lines that look like更改看起来像的线条
new XYChart.Data("2015", 70)
to specify an inferred generic type using the "diamond operator" <>
.使用“菱形运算符”
<>
指定推断的泛型类型。 Like,喜欢,
new XYChart.Data<>("2015", 70)
and if using an older version of Java (without the diamond operator), provide the <X,Y>
type parameters documented in XYChart.Data
like如果使用旧版本的 Java(没有菱形运算符),请提供
XYChart.Data
记录的<X,Y>
类型参数,如
new XYChart.Data<String, Integer>("2015", 70)
The following gives no warnings with java 12以下对 java 12没有警告
XYChart.Series<String, Integer> series1 = new XYChart.Series<>();
ObservableList<XYChart.Data<String, Integer>> list = FXCollections.observableArrayList();
list.add(new XYChart.Data<>("2015", 70));
list.add(new XYChart.Data<>("2016", 40));
list.add(new XYChart.Data<>("2017", 50));
list.add(new XYChart.Data<>("2018", 30));
series1.setData(list);
See my last comment to Elliot's answer.请参阅我对 Elliot 回答的最后一条评论。 Basically you can't get rid of the warning (other than using the annotation
@SuppressWarning
) when using method addAll()
.基本上,在使用
addAll()
方法时,您无法摆脱警告(除了使用注释@SuppressWarning
addAll()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.