How to check null element if it is integer array in Java?
Posted
by masato-san
on Stack Overflow
See other posts from Stack Overflow
or by masato-san
Published on 2010-06-13T19:39:21Z
Indexed on
2010/06/13
19:42 UTC
Read the original article
Hit count: 128
I'm quite new to Java and having an issue checking null element in integer array. I'm using Eclipse for editor and the line that checks null element is showing error:
Line that complains:
if(a[i] != null) {
Error msg from Eclipse:
The operator != is undefined for the argument type(s) int, null
In PHP, this works without any problem but in Java it seems like I have to change the array type from integer to Object to make the line not complain (like below)
Object[] a = new Object[3];
So my question is if I still want to declare as integer array and still want to check null, what is the syntax for it?
Code:
public void test() {
int[] a = new int[3];
for(int i=0; i<a.length; i++) {
if(a[i] != null) { //this line complains...
System.out.println('null!');
}
}
}
© Stack Overflow or respective owner