0

我正在尝试编写一个层次块,将元素放入一个 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;
}
4

3 回答 3

0

因此,在查看了关于 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");

            }
        }
    }       
}
};

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

于 2015-03-25T14:48:30.003 回答
0

我实际上不遵循:

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

现在关于那个 WTF 标记 - 你正在sencencesentences. 什么更好,我没有看到声明,sentences但我想它只是空的,对吗?这就是为什么你总是看到它是空的。至于我,里面有很多奇怪的代码和无用的注释。

于 2015-03-23T13:03:38.900 回答
0

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

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

于 2015-03-23T13:24:35.733 回答