如何检测array里的空白?
比如a[8]={1,2,3,4} 我如何知道a[6]这个空间是空白的, 我写的是 if(a[6]==null), 但在编译的时候出现这个错误, incomparable types: int and <nulltype>, 那我应该怎么改呢?
2010-10-10 03:43
2010-10-10 20:53
2010-10-10 20:56
2010-10-10 21:03
程序代码:
public static void main(String args[]) {
Integer a[]=new Integer[8];
a[0]=1;
a[1]=2;
a[2]=3;
if(a[6]==null){
System.out.println("a[6] is null");
}
}
2010-10-10 22:55
2010-10-11 01:24
程序代码:public class MyArray {
private int[] array;
private int size = 0;
private int max = 10;
private final int increase = 10;
public String toString() {
String str = "{ ";
for (int i = 0; i < size-1; i++) {
str += array[i] + ", ";
}
return str + array[size-1]+" }";
}
public void add(int num){
if(size<max){
max +=increase;
int[] temp = new int[max];
for(int i = 0;i<size;i++){
temp[i] = array[i];
}
array = temp;
}
array[size] = num;
size++;
}
public static void main(String[] args) {
MyArray myArray = new MyArray();
for(int i =0;i<15;i++){
myArray.add(i+1);
}
System.out.println(myArray);
}
}
2010-10-11 10:06