[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Array isEmpty

Posted on 2006-10-25
4
Medium Priority
?
1,825 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:basirana
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 2000 total points
ID: 17804634
1. You can check whether the array is null or the array's length is zero.

e.g.
boolean isEmpty(String[] array) { return array == null || array.length == 0; }

2. To initialize array as null, just set it as null.
String[] array = null;
0
 

Author Comment

by:basirana
ID: 17804694
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.
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 17804752
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
0
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 17804764
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
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question