将字符串转换为字符用法的JavaFX图表

时间:2017-04-27 19:16:52

标签: java

我正在尝试用Java创建一个程序来显示多少 每个字符都使用BarChart包含在TextArea中。

我写了一个返回" XYChart.Series"的方法。含有 字符以及在TextArea中使用的次数,

但是,每次通过按钮事件调用它时,它都会挂钩到 它只是将错误返回到

的曲调
"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
}

我得到的错误就在这一行

            s.getData().add(character, usage); // adds character and times used to series

这里有什么问题?

1 个答案:

答案 0 :(得分:0)

您正在呼叫List::add(int index, E element),请尝试此操作。

s.getData().add(new XYChart.Data(String.valueOf(character), usage));