ArrayList 不能转换为 java.util.ArrayList,toArray returns 什么都没有

ArrayList cannot be cast to java.util.ArrayList, toArray returns nothing

我正在尝试编写一个分层块,将元素放入一个 ArrayList 中,所有元素都属于一个更大的 ArrayList。该块所做的是采用现有的文本输入,并将每一行文本添加到 ArrayList 的元素中。然后将每一行创建为字符串的 ArrayList,每个字符串都是该行上的一个单词(我使用空格处的字符串拆分 (" ") 来执行此操作)。

我的问题是在尝试创建它时我需要使用 Arrays.asList(因为字符串拆分 return 是一个列表)

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> text = LinetoList(area);
        //"text" is a String ArrayList of every "line" in a piece of text
        //LinetoList is a method that returns an ArrayList based on each new line
        ArrayList<ArrayList> words = new ArrayList();
        for (int i = 0;  i < text.size(); i++) {
            ArrayList sentence =  (ArrayList) Arrays.asList(text.get(i).split(" "));
            /*Sentence is currently a list, however, changing the type to an Array 
             * or Arraylist Changes nothing */
            words.add(sentence);
            }
        for (int k = 0; k < words.size(); k++) {
            for (int i= 0; i < words.get(k).size(); i ++) {
                System.out.println(words.get(k).get(i));
            }
        }
    }
};

这是我的原始方法,但 return 出现了错误。此后我稍作调整,不再 return 是一个错误,但是 return 什么都不是。

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> text = LinetoList(area);
        //"text" is a String ArrayList of every "line" in a piece of text
        //LinetoList is a method that returns an ArrayList based on each new "line"
        ArrayList<ArrayList> words = new ArrayList();
        for (int i = 0;  i < text.size(); i++) {
            ArrayList <String> sentences = new ArrayList();
            String sentence[] =  text.get(i).split(" ");
            sentence = sentences.toArray(sentence);
            /*Sentence is currently a list, however, changing the type to an Array 
             * or Arraylist Changes nothing */
            words.add(sentences);
            }
        if (words.size() ==  0)
        {System.out.println("Theres nothing here"); }
        else {
        for (int k = 0; k < words.size(); k++) {
            for (int i= 0; i < words.get(k).size(); i ++) {

                System.out.println(words.get(k).get(i));}

            }
        }
    }
};

非常感谢任何关于如何处理解决方案的反馈或想法。

编辑:有些人要求使用 LinetoList 函数。该程序的大部分使用字符串的 ArrayLists,这就是它在这里被大量使用的原因。

private static ArrayList<String> LinetoList(JTextArea textArea) {

  String s[] = textArea.getText().split("\r?\n");
    ArrayList<String>arrList = new ArrayList<>(Arrays.asList(s)) ;
    return arrList;
}

我实际上并没有关注:

   String sentence[] =  text.get(i).split(" "); // here you have array
    sentence = sentences.toArray(sentence); // WTF?

现在关于 WTF 标记 - 您正在用 sentences 的内容填充 sencence 数组。什么更好,我没有看到 sentences 的声明,但我猜它只是空的,对吧?这就是为什么你总是看到它是空的。至于我,里面有很多奇怪的代码和无用的注释。

1) Arrays.asList没有return java.util.ArrayList,如果你想让它成为ArrayList,那么你需要做ArrayList sentence = new ArrayList( Arrays.asList(text.get(i).split(" ")));

2) toArray 方法将集合中的所有元素放入数组中。在您的情况下,集合是空的,因为您几乎在调用方法

之前创建

所以在查看了关于 toArray 和 asList 工作原理的评论后,我尝试了一种更多基于我的 linetolist 函数的不同方法

结果是

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<ArrayList> words = new ArrayList();
        ArrayList<ArrayList> splitLines = new ArrayList();
        ArrayList<String> characters = new ArrayList();
        //^^goes to (As hierarchey )
        ArrayList<String> text = LinetoList(area);
        //text is a String ArrayList of every line in a text
        //LinetoList is a method that returns an ArrayList based on each new line
        for (int i = 0;  i < text.size(); i++) {
            //for each line we have
            String sentence[] =  text.get(i).split(" ");
            ArrayList<String> splitText = new ArrayList<>(Arrays.asList(sentence));
            words.add(splitText);
            }
        for (int j = 0; j < words.size(); j++) {
            String sentence [] = text.get(j).split(" ");
            ArrayList<String> splitText = new ArrayList<>(Arrays.asList(sentence));
            ArrayList<ArrayList> SplitWords = new ArrayList();
            for (int i =0; i < sentence.length; i++) {
                ArrayList<Character> SplitCharacters = new ArrayList<Character>();
                for (int k = 0; k < splitText.get(i).length(); k ++) {

                SplitCharacters.add(splitText.get(i).charAt(k));

                }
                SplitWords.add(SplitCharacters);
            }
            splitLines.add(SplitWords);

        if (words.size() ==  0)
        {System.out.println("Theres nothing here"); }
        else {
        for (int k = 0; k < words.size(); k++) {
            System.out.println("\n");
            for (int i= 0; i < words.get(k).size(); i ++) {
                System.out.println(words.get(k).get(i));
                }

            }
        System.out.println("That was the original method \n");
        for (int k = 0; k < splitLines.size(); k++) {
            System.out.println(splitLines.get(k).toString() + "\n");

            }
        }
    }       
}
};

我包括了两种不同的方法来解决这个问题,一种将事情简化为单词的数组列表,另一种给出字符数组列表的数组列表(或包含字母的单词数组列表) 感谢所有的帮助。希望其他人可以从我的失误中吸取教训。