Solved

Array isEmpty

Posted on 2006-10-25
4
1,769 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
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
Peter Kwan earned 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How Complex Is This Java Course ? 9 63
word0 challenge 3 58
wordmultiple challenge 12 90
eclipse java  build path 6 21
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now