簡體   English   中英

如何將jTextArea掃描到arraylist或2d數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM