Link to home
Start Free TrialLog in
Avatar of basirana
basirana

asked on

Array isEmpty

Hi

I am using a normal Array not util.collections

String[] animals = { "bear", "cougar", "wolverine"};

I have two questions. How to find array is empty? How to intialize array as null(without any elements).

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Peter Kwan
Peter Kwan
Flag of Hong Kong image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of basirana
basirana

ASKER

But for this I need to create a method isEmpty(String[] array).
Is there anyway we can check inside if condition and find.

Thanks for ur reply.
You can just copy the condition to your if statement. Then you don't need the method.

i.e.
if (array == null || array.length == 0)
   // array is empty
else
  // array is not empty
of course you need to substitute "array" with your variable. In your example:

if (animals == null || animals.length == 0)
   // for empty case
else
  // for not empty case