7

I think that all we need is

if (matrix.length == 0)

However, I saw some of them write

if (matrix == null || matrix.length == 0 || matrix[0].length == 0) 

Is my version enough to check a matrix is empty or not or we need to write the above version?

KKKK
  • 347
  • 7
  • 18

1 Answers1

6

You will get a NullPointerException if you call matrix.length() if matrix is null, that's why the second check is better.

Vucko
  • 7,371
  • 2
  • 27
  • 45