We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,451 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

check whether an element array is null

hello there, is there anyone who can tell me hwo to actually check whether an an int array element is a null or not? in java, I tried (array[i]==null) or (array[i].equals("")) but it seems that the compiler will tell me that int is incomparable to null or string everytime I used those two to check whether my int array element is a null or not. Besides, I also used (array[i]==0), but this cant really check whether the int array element is null or not right? So I am wondering is there anyone that can help me to solve this problem.

3
Contributors
3
Replies
11 Hours
Discussion Span
9 Months Ago
Last Updated
4
Views
jjjmikalord
Newbie Poster
1 post since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

an int is a primitive, not an object. an int's default value is 0. of course, the array itself is an object, so you can check whether or not the array is null.

since an int cannot be null, no, you can't, but, if for instance, your elements are limited to a certain group of values, let's say, only the positive numbers, you can initialize your array and give all the elements a default value of -1, so, you can check whether the element == -1 or not.

if this is not enough, create an array of Integers instead of an array of ints.

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

Create an array of Integers instead of an array of ints. Java 1.5+ supports autoboxing and unboxing so an array of Integer will accept int values.

rajesh1158
Junior Poster in Training
59 posts since Oct 2010
Reputation Points: 4
Solved Threads: 1
Skill Endorsements: 0

it won't 'accept int values', it will, however, through the autoboxing mechanism you've mentioned, transfer int variables to Integers, which then are accepted.

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0655 seconds using 2.74MB