-2

How it is possible to initialize an array in this form with null values in Java?

int array[][] = {
            {1, 6, 4, 1,-1},
            {6, 3, 3, 3, 9},
            {6, 3, 3, 3, 9},
            {6, 3, 3, 3, 9},
            {6, 3, 3, 3, 9}
    };

I tried that, but it doesn't work (is that possible?):

int array[][] = {
            {1, 6, 4, 1,-1},
            {6, null, 3, 3, 9},
            {6, 3, 3, 3, 9},
            {6, 3, 3, null, 9},
            {6, 3, 3, 3, 9}
    };

Thanks

1 Answers1

3

It's only possible with arrays of reference types :

Integer array[][] = {
        {1, 6, 4, 1,-1},
        {6, null, 3, 3, 9},
        {6, 3, 3, 3, 9},
        {6, 3, 3, null, 9},
        {6, 3, 3, 3, 9}
};

Primitive types can't be assigned null, so primitive arrays, such as int arrays, can't contain nulls.

Eran
  • 359,724
  • 45
  • 626
  • 694