清除二维数组

时间:2012-10-18 00:32:11

标签: java multidimensional-array clear

如何清除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();
}

3 个答案:

答案 0 :(得分:1)

遍历数组并将每个元素设置为null。您可以使用java.utils.Arrays实用程序类使事情更清洁/更整洁。

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

答案 1 :(得分:0)

像...一样的东西。

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;

答案 2 :(得分:0)

这是一种解决方案:

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