Two Dimensional Arrays in Java Lang
My question is,can i check a place if null,or set a place to null in two dimensional String array with my way.
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
İs it possible?Or if wrong,how to do it.?Regards..
Yes. Since this is an array of objects (which is really just an array of memory addresses, you can indeed set them to null as that would be setting the address to 0). However if you have a two dimensional array of a primitive type, such as an int, then you cannot set positions to null.
You need to specify how many elements are in your array, and you don't have to manually set the elemnts to 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 ("-");
}
}
精彩评论