【发布时间】:2010-06-13 19:39:21
【问题描述】:
我是 Java 新手,在检查整数数组中的空元素时遇到问题。 我正在使用 Eclipse 作为编辑器,检查 null 元素的行显示错误:
抱怨的行:
if(a[i] != null) {
来自 Eclipse 的错误消息:
The operator != is undefined for the argument type(s) int, null
在 PHP 中,这没有任何问题,但在 Java 中,我似乎必须将数组类型从整数更改为对象,以使该行不抱怨(如下所示)
Object[] a = new Object[3];
所以我的问题是,如果我仍然想声明为整数数组并且仍然想检查 null, 它的语法是什么?
代码:
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!');
}
}
}
【问题讨论】: