473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Checking if an array is null or not

I created an array of books and named it catalog as follows:

Book[] catalog;
catalog = new Book[20];

Based on the input command, I can either list this catalog or add items to the catalog. When I list this catalog, how can I check if this catalog is null or not?

Thanks for your help
Oct 30 '07 #1
7 1381
Shashi Sadasivan
1,435 Expert 1GB
I created an array of books and named it catalog as follows:

Book[] catalog;
catalog = new Book[20];

Based on the input command, I can either list this catalog or add items to the catalog. When I list this catalog, how can I check if this catalog is null or not?

Thanks for your help
Hi,
once u create catalog array, each elemnt is assigned to be null (as it is your own predefined class)
so if you want to check if an element is null
Expand|Select|Wrap|Line Numbers
  1. if(catalog[0] == null)
will do the trick
Oct 30 '07 #2
Plater
7,872 Expert 4TB
For the situtation:

Expand|Select|Wrap|Line Numbers
  1. Book[] mybooks;
  2.  
If you were to check mybooks[0] you would get an exception.
Use
Expand|Select|Wrap|Line Numbers
  1. if (mybooks==null)
  2.  
However, I generally make it a point to assign things like:
Expand|Select|Wrap|Line Numbers
  1. Book[] mybooks= new Book[0];
  2.  
Then I can do this:
Expand|Select|Wrap|Line Numbers
  1. if (mybooks.Length==0)
  2.  
to determine if it's "empty"
Oct 30 '07 #3
Shashi Sadasivan
1,435 Expert 1GB
For the situtation:

Expand|Select|Wrap|Line Numbers
  1. Book[] mybooks;
  2.  
If you were to check mybooks[0] you would get an exception.
Use
Expand|Select|Wrap|Line Numbers
  1. if (mybooks==null)
  2.  
However, I generally make it a point to assign things like:
Expand|Select|Wrap|Line Numbers
  1. Book[] mybooks= new Book[0];
  2.  
Then I can do this:
Expand|Select|Wrap|Line Numbers
  1. if (mybooks.Length==0)
  2.  
to determine if it's "empty"
Hi Plater
if you do
Expand|Select|Wrap|Line Numbers
  1. Book[] mybooks= new Book[10];
mybooks.Length will result in 10 regardless of wether you have assigned any array items with any objects.
this code creates an aray of 10 null elements
However if it was an int, or frloat array it would have made an array with 10 elements each set to 0
I did try it to confirm it.
However using a List<T> or a dynamic array will give a better functionality
Oct 30 '07 #4
r035198x
13,262 8TB
Hi Plater
if you do
Expand|Select|Wrap|Line Numbers
  1. Book[] mybooks= new Book[10];
mybooks.Length will result in 10 regardless of wether you have assigned any array items with any objects.
this code creates an aray of 10 null elements
However if it was an int, or frloat array it would have made an array with 10 elements each set to 0
I did try it to confirm it.
However using a List<T> or a dynamic array will give a better functionality
He used zero for the intial size of his array not 10 (ten).

Arrays are so Fortranesque. Use them only when the number of objects to be stored is known before hand. Then you only need to intialize the array once with the appropriate size. Otherwise ArrayList is the way to go.
Oct 31 '07 #5
Shashi Sadasivan
1,435 Expert 1GB
He used zero for the intial size of his array not 10 (ten).

Arrays are so Fortranesque. Use them only when the number of objects to be stored is known before hand. Then you only need to intialize the array once with the appropriate size. Otherwise ArrayList is the way to go.
Hi,
i think i should have had my post more subtle, didnt want to offend, but what i meant was that by using the length you cannot determine the number of elemnts which have been assigned.

apologies for any harh looking reply i had in there...i have no such intensions!! :)
Oct 31 '07 #6
r035198x
13,262 8TB
Hi,
i think i should have had my post more subtle, didnt want to offend, but what i meant was that by using the length you cannot determine the number of elemnts which have been assigned.

apologies for any harh looking reply i had in there...i have no such intensions!! :)
Of course it doesn't look harsh at all. You just overlooked that he had used zero to initialize the array so the length would be zero like he expected.
The approach is not a very good one of course because he creates a useless array, reserving unneccessary memory which has to be gced when he finally initializes the array to the proper sized array.
Oct 31 '07 #7
Plater
7,872 Expert 4TB
Hi,
i think i should have had my post more subtle, didnt want to offend, but what i meant was that by using the length you cannot determine the number of elemnts which have been assigned.

apologies for any harh looking reply i had in there...i have no such intensions!! :)
But using myBooks[0] when it was delcared as
"Books[] myBooks;"
or
"Books[] myBooks = new Books[0];"
would throw an exception.
You would want to check for null on the object FIRST, before checking for null on their individual indexes.
And declaring an "empty" array is done with setting the size to [0] since you cann't assign "null" to an array declaration explicitly.
Oct 31 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tom Esker | last post by:
I've got Javascript in a form that adds up all of the numbers in a column of form fields and displays a total. It works great if every field has an initial value of 0, but if any of them are null,...
14
by: Kayle | last post by:
How should we check if the '\0' characters exists in the string as I am confused that some books mentioned that we have to check whether we need to make sure that we pass the...
30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
6
by: Flip | last post by:
I'm reading the O'Reilly's Progamming C# book and I have a question about array bounds checking. On page 174, near the top, they show an example where c# does indeed to array bounds checking cause...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
21
by: jacob navia | last post by:
Many compilers check printf for errors, lcc-win32 too. But there are other functions that would be worth to check, specially memset. Memset is used mainly to clear a memory zone, receiving a...
4
by: Patient Guy | last post by:
Does anyone have any coding rules they follow when doing argument checking? When arguments fail during check, do you return from the call with an ambiguous return value, or do you throw...
8
by: Fuzzydave | last post by:
Okay, I have been handed a python project and working through it I have had to add a report. I am returning 10 variables the results of an SQL Query and as usual the number of results vary from...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.