[英]how can I scan jTextArea to arraylist or 2d array
我有一个jTextArea
具有此值[1,3,5,5,5,6],[5,1,4,3,3,3],[3,5,6,5,5,4]
我的问题是如何将jTextArea中的数据放入2d数组或Arraylist?
我的代码:
public void toList(JTextArea textArea){
ArrayList<String>arrList = new ArrayList<>(Arrays.asList(jTextArea1.getText())) ;
arrList.stream().forEach((jh) -> {
System.out.println(jh);
});
}
但它不返回任何输出。
您需要将String
拆分成单个组件
[1,3,5,5,5,6]
[5,1,4,3,3,3]
[3,5,6,5,5,4]
然后将每个“块”分解为一系列单独的数字
1
3
5
5
5
6
依此类推,例如...
// Test value
String text = "[1,3,5,5,5,6],[5,1,4,3,3,3],[3,5,6,5,5,4]";
// Break down the outter blocks [...]
String[] blocks = text.split("],");
// Build the array....
String[][] blocksArray = new String[blocks.length][];
for (int index = 0; index < blocks.length; index++) {
// Get the next block [...]
String block = blocks[index];
// Remove the brackets [ and ]
block = block.replace("[", "").replace("]", "");
// Break the numbers apart....
String[] elements = block.split(",");
// Apply the numbers to the block array
blocksArray[index] = elements;
}
使用...
for (String[] block : blocksArray) {
System.out.println(Arrays.toString(block));
}
我知道了
[1, 3, 5, 5, 5, 6]
[5, 1, 4, 3, 3, 3]
[3, 5, 6, 5, 5, 4]
注意Arrays.toString
添加[
和,
和]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.