0

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..

2
  • 1
    You would know if you try it. Commented Apr 29, 2011 at 14:32
  • Did you try it and it didn't work? If not, then it should, and something else may be the problem. Commented Apr 29, 2011 at 14:32

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i cannot apply to strings as well then?..so how i can do it.deleting it with for loop?
It will work fine for String objects. Any standard type that starts with a capital letter is an object (and you should follow this convention for your own classes). Any standard type that starts with a lower-case letter, such as int or double, is a primitive.
0

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 ("-");  
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.