How to verify is an array of cells is null in java?
The "null" character is '\0'
, so you can compare if grid[i][j] == '\0'
.
The literal null
is for reference types, which char
is not.
Primitive arrays such as your char[][]
can't contain null
. Only object arrays can hold nulls. You could convert your array to Character[][]
instead.
Thanks to autoboxing your existing code should work just fine, but now you can actually put nulls in it.
In my case that would mean null because I initialize an empty char array
Incorrect. The default value of a char
is '\0'
, e.g. new char[3][4]
creates an outer arrays of 3 sub-arrays, and each sub-array contains 4 '\0'
values.
So, your if
statement should be:
if (grid[i][j] == '\0')