tukevaseppo
tukevaseppo

Reputation: 21

How to check for an empty 2d-array?

In java language, how could I change the condition so that it returns as false instead of true?

char table[][] = {{}};
return (table != null && table.length > 0);

Upvotes: 1

Views: 479

Answers (3)

user14940971
user14940971

Reputation:

The universal way to reverse any condition is to use a round brackets and an exclamation mark !(...). If this condition returns true:

return table != null && table.length > 0;

then this condition returns false:

return !(table != null && table.length > 0);

Upvotes: 0

mistahenry
mistahenry

Reputation: 8724

The first question you need to answer to yourself is "What is an empty 2d array?". As you have it now, that is either an uninitialized array or an initialized array that contains no elements.

Instead, you have initialized a 2d array to be an array holding an empty array with char table[][] = {{}};

char table[][] = {{}};
System.out.println(table.length);

would print 1 because the element at index 0 is an array of length 0.

Upvotes: 3

reelpro7on
reelpro7on

Reputation: 3

if(table[0].length==0)

This should do

Upvotes: -2

Related Questions