簡體   English   中英

清除二維數組

[英]Clear Two Dimensional Array

如何清除 6x6“表”,以便清除其中的任何內容? (我已經用 ActionListener 制作了 clearbutton ......等)

        //other code above that creates window, below is the code that creates the table I need to clear

         square = new JTextField[s][s];
    for (int r=0; r!=s; r++) {
        symbols[r] = new JTextField();
        symbols[r].setBounds(35+r*35, 40, 30, 25);
        win.add(symbols[r], 0);
        for (int c=0; c!=s; c++) {
            square[r][c] = new JTextField();
            square[r][c].setBounds(15+c*35, 110+r*30, 30, 25);
            win.add(square[r][c], 0);
        }
    }
    win.repaint();
}

循環遍歷數組並將每個元素設置為 null。 您可以使用java.utils.Arrays實用程序類使事情更干凈/更整潔。

for( int i = 0; i < square.length; i++ )
   Arrays.fill( square[i], null );

就像是...

for (int index = 0; index < square.length; index++) {
    square[index] = null;
}
square = null;

會做更多的伎倆(實際上最后一行通常就足夠了)......

如果你真的偏執...

for (int index = 0; index < square.length; index++) {
    for (int inner = 0; inner < square[index].length; inner++) {
        square[index][inner] = null;
    }
    square[index] = null;
}
square = null;

這是一行解決方案:

Arrays.stream(square).forEach(x -> Arrays.fill(x, null));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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