Ertan Hasani
Ertan Hasani

Reputation: 817

Is it possible to create an array of XYChart.Series on JavaFX?

I want to know if I can create an array of XYChart.Series<String, Number> on JavaFX?

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.

This is what I tried:

List<XYChart.Series<String, Number>> seriesList = new ArrayList<XYChart.Series<String, Number>>();

Because I had no idea how to do it otherways.

Can you please help me?

Upvotes: 0

Views: 2447

Answers (1)

fabian
fabian

Reputation: 82531

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];

Update

Array elements still need to be initialized this way since arrays generated this way are filled with null elements.

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);

Upvotes: 1

Related Questions