gpt4 book ai didi

java - 实时图表 - ArrayList 转换错误

转载 作者:行者123 更新时间:2023-11-30 02:37:32 25 4
gpt4 key购买 nike

我目前正在使用 XYChart 在 java 中创建图形,我使用以下代码来创建图表:

    stage.setTitle("Emotion Analyser");

final CategoryAxis xAxis = new CategoryAxis();
final NumberAxis yAxis = new NumberAxis();

xAxis.setLabel("Utterances");

final LineChart<String,Number> lineChart =
new LineChart<String,Number>(xAxis,yAxis);

lineChart.setTitle("Emotion Analysis");

XYChart.Series series1 = new XYChart.Series();
series1.setName("Happy");

series1.getData().add(new XYChart.Data("1st", 23));
series1.getData().add(new XYChart.Data("2nd", 14));
series1.getData().add(new XYChart.Data("3rd", 15));
series1.getData().add(new XYChart.Data("4th", 24));
series1.getData().add(new XYChart.Data("5th", 34));
series1.getData().add(new XYChart.Data("6th", 36));
series1.getData().add(new XYChart.Data("7th", 22));
series1.getData().add(new XYChart.Data("8th", 45));
series1.getData().add(new XYChart.Data("9th", 43));
series1.getData().add(new XYChart.Data("10th", 17));

XYChart.Series series2 = new XYChart.Series();
series2.setName("sad");
series2.getData().add(new XYChart.Data("1st", 33));
series2.getData().add(new XYChart.Data("2nd", 34));
series2.getData().add(new XYChart.Data("3rd", 25));
series2.getData().add(new XYChart.Data("4th", 44));
series2.getData().add(new XYChart.Data("5th", 39));
series2.getData().add(new XYChart.Data("6th", 16));
series2.getData().add(new XYChart.Data("7th", 55));
series2.getData().add(new XYChart.Data("8th", 54));
series2.getData().add(new XYChart.Data("9th", 48));
series2.getData().add(new XYChart.Data("10th", 27));

XYChart.Series series3 = new XYChart.Series();
series3.setName("Shocked");

series3.getData().add(new XYChart.Data("1st", 44));
series3.getData().add(new XYChart.Data("2nd", 35));
series3.getData().add(new XYChart.Data("3rd", 36));
series3.getData().add(new XYChart.Data("4th", 33));
series3.getData().add(new XYChart.Data("5th", 31));
series3.getData().add(new XYChart.Data("6th", 26));
series3.getData().add(new XYChart.Data("7th", 22));
series3.getData().add(new XYChart.Data("8th", 25));
series3.getData().add(new XYChart.Data("9th", 43));
series3.getData().add(new XYChart.Data("10th", 44));

Scene scene = new Scene(lineChart,800,600);
lineChart.getData().addAll(series1, series2, series3);

stage.setScene(scene);
stage.show();

图片:enter image description here这是当您没有所有数据时想要手动添加它们的时候...但是我正在尝试创建一个实时更新图,它不知道每次运行时会有多少行,因此第一次运行时图表上可能有 2 条线,第二次运行时可能有 6 条线。

所以我想以某种方式使用 ArrayList,例如:

arraylist

并执行与之前相同的操作,

我尝试这样做:

list.get(0).setName("Happyq");

但这没有用

错误:

Executing /

最佳答案

错误

Caused by java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

表示当列表为空 ( list.get(0) ) 时,您正在尝试访问列表的第一个元素(例如,通过调用 Size: 0 )。

堆栈跟踪告诉您这发生在 GraphTry1.java 的第 42 行.

有关读取和解释堆栈跟踪的更多信息,请参阅 this question .

关于java - 实时图表 - ArrayList 转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42648309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com