【发布时间】:2018-07-15 10:24:57
【问题描述】:
我遵循了这个线程中提出的观点,但我收到错误“操作符未定义参数类型[...]”
How can I check whether an array is null / empty?
我的代码:
public class Test{
private int[] array = new int [5];
public int method(int i) {
for(int s = 0; s <= array.length; s++) {
if( array[s] != null) { //I get the error in here even though the highest upvoted answer in the linked question recommends this solution. I obviously cant check it for 0, because the user input can be 0.
array[s] = i;
} else {
method(i);
}
}
}
}
谢谢!
【问题讨论】:
-
s <= array.length是总是错误的,因为您将尝试访问超出数组的边界(导致异常) -
另外,
int[]中不能有“空”值 - 默认值为 0,无法将其设置为null。您的数组可以是null或length为 0,您无能为力 -
是的,没有想到这一点,但即使使用 array.length -1 修复它,我的其他问题仍然存在。