我正在尝试编写一个层次结构块,它将元素放入一个ArrayList中,而不是更大的ArrayList。该块的作用是获取现有的文本输入,并将每行文本添加到ArrayList的元素中。然后将每一行创建为字符串的ArrayList,每个String都是该行上的一个单词(我在空格(“”)处使用字符串拆分来执行此操作)。
我的问题是在尝试创建时我需要使用Arrays.asList(因为String Split返回一个List)
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;
}
答案 0 :(得分:0)
我实际上并没遵循:
String sentence[] = text.get(i).split(" "); // here you have array
sentence = sentences.toArray(sentence); // WTF?
现在关于WTF标记 - 您正在填充内容为sencence
的{{1}}数组。什么是更好,我没有看到sentences
的声明,但我想它只是空的对吗?这就是你为什么一直看空的原因。至于我,很多奇怪的代码和无用的评论。
答案 1 :(得分:0)
1)Arrays.asList
不返回java.util.ArrayList
,如果你想让它成为ArrayList,那么你需要做ArrayList sentence = new ArrayList( Arrays.asList(text.get(i).split(" ")));
2)toArray
方法将集合中的所有元素放入数组中。在你的情况下,在你调用方法
答案 2 :(得分: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(或者是一个包含字母的数组的单词列表) 谢谢你的帮助。希望其他任何人都可以从我的错误中拿走一些东西。