Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a 2D matrix is empty?

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?

like image 391
KKKK Avatar asked Mar 13 '16 20:03

KKKK


1 Answers

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

like image 188
Vucko Avatar answered Oct 21 '22 04:10

Vucko