【问题标题】:ArrayList cannot be cast to java.util.ArrayList, toArray returns nothingArrayList 无法转换为 java.util.ArrayList,toArray 不返回任何内容
【发布时间】:2015-03-23 12:43:31
【问题描述】:

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

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

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

这是我返回错误的原始方法。从那以后我稍微调整了一下,它不再返回错误但是什么都没有返回。

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

【问题讨论】:

  • 1) String.split() 返回 Array[] 而不是 List
  • 反馈:考虑重命名变量。 sentence 是一个 ArrayList,然后 sentence 是一个数组……我发现这很混乱。含义:尽量避免使用几乎相同的变量名。
  • 使用ArrayList&lt;String&gt; sentence = new ArrayList&lt;&gt;(Arrays.asList(sentences));,然后使用words.addAll(sentence)。您也可以只使用 List 代替,因为无论如何附加到单词后它似乎超出了范围。
  • 能看到LineToList函数吗?

标签: java list arraylist jtextarea toarray


【解决方案1】:

我实际上并没有关注:

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

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

【讨论】:

  • 这是在阅读 toArray 函数如何工作时的一个误解。我认为它的工作方式类似于 asList,因为它只是将一个数据集转换为另一种类型(因此,列表将转换为“数组”)。对这种类型进行了更多的实验,因此更彻底(如果有点生硬)的解释会有所帮助。我发现 java 文档描述没有彻底描述该功能的工作原理或实现方式。对这种方法的其他版本有什么想法吗?我列出了两个,知道他们都有问题。我觉得另一个对它的组成部分缺乏了解。
【解决方案2】:

1)Arrays.asList不返回java.util.ArrayList,如果你想要它是ArrayList,那么你需要做ArrayList sentence = new ArrayList( Arrays.asList(text.get(i).split(" ")));

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

【讨论】:

  • 我做了更多的研究,我对 toArray 的原始阅读会将某些东西“转换为数组”。然而知道这一点,遍历列表中的每个元素并使用“toArray”来实际制作数组是多么可行。
【解决方案3】:

因此,在查看了有关 toArray 和 asList 工作原理的 cmets 之后,我尝试了一种不同的方法,该方法更多地基于我的 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");

            }
        }
    }       
}
};

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-29
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 2015-07-16
    • 1970-01-01
    • 2020-05-15
    相关资源
    最近更新 更多