Java语言中的二维数组

Java语言中的二维数组,java,multidimensional-array,Java,Multidimensional Array,我的问题是,我可以检查一个空的位置吗,或者用我的方法在二维字符串数组中设置一个空的位置 String [][] xx=new String [][] public void set(int a,int b) { xx[a][b]=null;//setting null } . . . . . if(xx[a][b]==null)//check if null ///some codes İ有可能吗?或者如果错了,怎么做。?关于..是的。因为这是一个对象数组(实际上只是

我的问题是,我可以检查一个空的位置吗,或者用我的方法在二维字符串数组中设置一个空的位置

String [][] xx=new String [][]
public void set(int a,int b)
  {
 xx[a][b]=null;//setting null
 }
 .
 .
 .
 .
 .
 if(xx[a][b]==null)//check if null
  ///some codes 

İ有可能吗?或者如果错了,怎么做。?关于..

是的。因为这是一个对象数组(实际上只是一个内存地址数组,您确实可以将它们设置为null,因为这将地址设置为0)。但是,如果您有一个基本类型的二维数组,如int,则不能将位置设置为null。

您需要指定数组中的元素数量,并且不必手动将元素设置为null

public class Dim2AR
{
    // String [][] xx = new String [][];
    String [][] xx = new String [20][20];

    public void set (int a, int b)
    {
        xx[a][b] = null; 
    }

    public void set (int a, int b, String v)
    {
        xx[a][b] = v; 
    }

    public Dim2AR ()
    {
        check (3, 3);
        set (3, 3);
        check (4, 3);
        set (4, 3, "foo");
        check (4, 3);
    }

    public static void main (String args[])
    {
        new Dim2AR ();
    }

    public void check (int a, int b)
    {
        if (xx[a][b] != null) System.out.println ("> " + xx[a][b]);
        else System.out.println ("-");  
    }
}

如果你试一下,你就会知道。你试过了吗,但没有成功?如果不是,那么应该是这样,可能还有其他问题。我不能同样应用于字符串?。那么我如何才能做到。使用for循环删除它?这对字符串对象很有效。任何以大写字母开头的标准类型都是对象(对于自己的类,应该遵循此约定)。任何以小写字母开头的标准类型,如
int
double
,都是原语。