问题描述
我想知道是否可以在JavaFX上创建XYChart.Series<String, Number>
的数组吗?
I want to know if I can create an array of XYChart.Series<String, Number>
on JavaFX?
因为我需要为数据库中的每个人创建一个XYChart.Series
,所以我尝试为我的LineChart
创建一个XYChart.Series<String, Number>
数组,但我做不到.
Because I need to create a XYChart.Series
for each person on database, and I tried to make an array of XYChart.Series<String, Number>
for my LineChart
but I couldn't.
这是我尝试过的:
List<XYChart.Series<String, Number>> seriesList = new ArrayList<XYChart.Series<String, Number>>();
因为我不知道该怎么做.
Because I had no idea how to do it otherways.
你能帮我吗?
推荐答案
您可以创建原始类型的数组,并将其分配给使用通用类型声明的变量.
You can create a array of the raw type and assign it to a variable declared with a generic type.
int size = ...
XYChart.Series<String, Number>[] seriesArray = new XYChart.Series[size];
更新
仍然需要以这种方式初始化数组元素,因为以这种方式生成的数组被null
元素填充.
Array elements still need to be initialized this way since arrays generated this way are filled with null
elements.
Java 8提供了一种创建&通过流API使用短代码初始化数组:
Java 8 provides a way create & initilize a array using short code via streams API:
XYChart.Series<String, Number>[] seriesArray = Stream.<XYChart.Series<String, Number>>generate(XYChart.Series::new).limit(size).toArray(XYChart.Series[]::new);
这篇关于是否可以在JavaFX上创建XYChart.Series数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!