Java JTextArea 行到 ArrayList<String>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20432404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Lines of JTextArea to an ArrayList<String>
提问by Mike
How would one convert every line in a JTextArea
into an ArrayList<String>
? That is, how does one detect line breaks in a JTextArea?
如何将 a 中的每一行转换JTextArea
为 an ArrayList<String>
?也就是说,如何检测 JTextArea 中的换行符?
Here is my current pseudo code implementation:
这是我当前的伪代码实现:
- get text from JTextArea
- Go through every char in JTextArea and adds it to a string called currentWord until it finds a line break char //(does such a thing exist?)
- When the loop detects a line break, it adds currentWord to the arrayList and sets currentWord to empty.
- After the loop ends, add currrentWord to the ArrayList.
- 从 JTextArea 获取文本
- 遍历 JTextArea 中的每个字符,并将其添加到名为 currentWord 的字符串中,直到找到换行符字符 //(这样的东西存在吗?)
- 当循环检测到换行符时,它会将 currentWord 添加到 arrayList 并将 currentWord 设置为空。
- 循环结束后,将 currrentWord 添加到 ArrayList。
1. Is there an easier way to do this?
2. Does this implementation scale well with size? (I'm guessing no)
1. 有没有更简单的方法来做到这一点?
2. 这个实现是否能随着规模的扩大而很好地扩展?(我猜没有)
回答by MadProgrammer
Start by using JTextArea#getText
to get ALL the text from the text area.
首先使用JTextArea#getText
从文本区域获取所有文本。
Once you have that, you can use String#split
, passing "\n"
as the value. This will return an array of String
's split on the new line.
一旦你有了它,你就可以使用String#split
,"\n"
作为值传递。这将String
在新行上返回一个's split数组。
If you want to extract each word from the line, you could split it again, presumably on " "
, which will give you each word.
如果您想从该行中提取每个单词,您可以再次拆分它,大概是在" "
,这将为您提供每个单词。
You can then use Arrays.asList
to convert this to a List
(of words or lines) and (assuming you've already created an instance of one), use ArrayList#addAll
to add all the String
s to the list.
然后,您可以使用Arrays.asList
将其转换为List
(单词或行)和(假设您已经创建了一个实例),用于ArrayList#addAll
将所有String
s添加到列表中。
If you're only interested in each unquie word, you could use a Set
to filter out duplicate words
如果你只对每一个 unquie 词感兴趣,你可以使用 aSet
来过滤掉重复的词
回答by Sage
embrace the power of Strig.split(regex)
and Arrays.asList
function:
拥抱以下的力量Strig.split(regex)
和Arrays.asList
功能:
String s[] = jTextArea1.getText().split("\r?\n");
ArrayList<String>arrList = new ArrayList<>(Arrays.asList(s)) ;
System.out.println(arrList);
回答by Elliott Frisch
Or, on reflection, I might use your original algorithm like this
或者,经过反思,我可能会像这样使用您的原始算法
private static List<String> toList(JTextArea textArea) {
List<String> al = new ArrayList<String>(); // return list.
if (textArea != null) {
String val = textArea.getText(); // the text.
StringBuilder sb = new StringBuilder(); // for the current word.
for (char c : val.toCharArray()) {
if (Character.isWhitespace(c)) { // ANY white space.
if (sb.length() > 0) { // is there a current word?
al.add(sb.toString()); // add it to the list.
sb.setLength(0); // clear the word.
}
} else {
sb.append(c); // NOT white space, add it to current word.
}
}
}
return al; // return the list.
}
回答by chackley
final List<String> lines = Arrays.asList(textArea.split("\n"));
回答by Luke
JTextArea txArea = new JTextArea();
txArea.setText("line1\nline2\nline3");
String txt = txArea.getText();
String [] arrayOfLines = txt.split("\n");
ArrayList<String> linesAsAL = new ArrayList<String>();
for(String line: arrayOfLines){
linesAsAL.add(line);
}
Or instead of adding lines to ArrayList on a loop something more elegant:
或者不是在循环中向 ArrayList 添加行,而是更优雅的东西:
List<String> lines = Arrays.asList(txt.split("\n"));
回答by SerefAltindal
String line[]=jTextArea1.getText().split("\n");
List<String> list=Arrays.asList(line);
回答by guest
scanner s = new scanner(JTextArea.getText());
while (s.hasNext()) {
arrayList.add(s.nextLine())
}