将字符串转换为字符用法的JavaFX图表
我正试图用Java编写一个程序来显示有多少将字符串转换为字符用法的JavaFX图表,java,Java,我正试图用Java编写一个程序来显示有多少 每个字符都包含在使用条形图的文本区域内 我编写了一个方法,返回一个“XYChart.Series”,其中包含 字符及其在文本区域内使用的次数 但是,每次通过按钮事件调用时,它都会连接到 它只是返回一个错误到 "java.lang.IndexOutOfBoundsException: Index: 100, Size: 0" 这是我写的方法 private XYChart.Series stringIntoSeries(String text) {
每个字符都包含在使用条形图的文本区域内 我编写了一个方法,返回一个“XYChart.Series”,其中包含
字符及其在文本区域内使用的次数 但是,每次通过按钮事件调用时,它都会连接到
它只是返回一个错误到
"java.lang.IndexOutOfBoundsException: Index: 100, Size: 0"
这是我写的方法
private XYChart.Series stringIntoSeries(String text)
{
char[] chars = text.toCharArray(); // char array of characters contained in string
List<List> usageList = new ArrayList(); // list to contain character usage
for(int i = 0; i < chars.length; i++)
{
char c = chars[i];
if(usageListHasChar(usageList, c)) // checks if character character has an entry in the list
{
int uI = usageListCharIndex(usageList, c); // finds index of the entry
List l = (List)usageList.get(uI);
l.set(1, (int)l.get(1)+1);
usageList.set(uI, l);
}
else // if not create new entry
{
List l = new ArrayList();
l.add(c);
l.add(1);
usageList.add(l);
}
}
XYChart.Series s = new XYChart.Series(); // new series to contain character usage
s.setName("");
for(int i = 0; i < usageList.size(); i++) // add characters and amount of times used
{
List l = usageList.get(i);
char character = (char)l.get(0);
int usage = (int)l.get(1);
s.getData().add(character, usage); // adds character and times used to series
}
return s; // returns the series once characters and usage are added which is then added to a BarChart
}
这里怎么了?您正在调用
List::add(int-index,E-element)
,请尝试此操作
s.getData().add(new XYChart.Data(String.valueOf(character), usage));
感谢您的帮助,有什么我可以改进的吗?我认为将
usageList
更改为Map
会使它成为一个更通用的实现。因为Map
可以比List
更快地找到索引。请参阅本文。
s.getData().add(new XYChart.Data(String.valueOf(character), usage));