• Post Reply Bookmark Topic Watch Topic
  • New Topic

empty array[][] contents  RSS feed

 
Krit Christoforou
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to check wether a "space" in an array[][] is occupied, so i need to check if its empty...

If i create a char[3][3], what will [1][1] contain for instance? How can i check if its not empty?
Thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For built-in types, like char, int, float, etc., this is tough. You will have to define what "empty" means. Since you are using a char array, are there specific characters you are using? What is the purpose of the array and what are the valid characters that can be stored in it? You can choose a different character to represent "empty".

On the other hand, it is easy to check if an element in an array of objects is "empty". You can do something like this:

I hope this helps. If you have more questions, please provide some more detail about what you are trying to do. That will help us be able to give you more accurate advice on how to solve your problem.

Layne
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A newly allocated char array will contain all 0's (i.e., char x = 0). So will int, short, long, float, and double arrays. boolean arrays will contains 'false', and all others will contain 'null'.
 
Krit Christoforou
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it, thanks..a char[][] array is full of 0's when its initialized, just as you said, so ==0 does the job
umm ==null wont do the job, it returns a compiler error
Thanks
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Krit Christoforou:
Got it, thanks..a char[][] array is full of 0's when its initialized, just as you said, so ==0 does the job
umm ==null wont do the job, it returns a compiler error
Thanks


As I mentioned earlier, == null will work if you use an array of Objects. Since char is a primitive == null won't work for an arry of chars.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!