Solved

Array isEmpty

Posted on 2006-10-25
4
1,797 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 46
web application structure 18 102
How to convert from xls to xlsx using java 7 57
Java basic valueOf question 1 29
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

820 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