整數數組元素是值類型(Int類型),因此它們將分配給它們的值存儲在內存位置中。如果你想僞造一個空值,你可以嘗試給元素賦值-1。
嘗試這樣的事情......
public static void main(String[]args){
int a[][]={{4,3,2,1},{0,-1,-1,-1},{0,-1,-1,-1}};
printStatus(a);
procesar(a);
printStatus(a);
}
public static void procesar (int a[][])
{
int temp, tope0, tope1, tope2;
tope0 = ((a[0].length)-1);
tope1 = 0;
tope2 = 0;
while(a[0][tope0] >= 0){
if(a[2][tope2]==0){
System.out.println(a[2][tope2]);
temp=a[0][tope0];
a[2][tope2] = temp;
a[0][tope0] = 0;
tope0= tope0 -1;
tope2 = tope2 +1 ;
System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1]);
printStatus(a);
}
System.out.println(a[0][tope0]+ " "+ a[2][(tope2-1)]);
if((a[2][(tope2 -1)]> a[0][tope0])){
temp=a[0][tope0];
a[2][tope2] = temp;
a[0][tope0] = 0;
tope0= tope0 -1;
tope2 = tope2 +1 ;
System.out.println(a[0][tope0]+ " "+a[1][tope1] +" "+ a[2][tope2 -1]);
printStatus(a);
}
if(a[1][tope1]==0){
System.out.println(a[1][tope1]);
temp = a[0][tope0];
a[1][tope1]= temp;
a[0][tope0]= 0;
tope0= tope0 -1;
tope1 = tope1 +1;
System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1]);
printStatus(a);
}
System.out.println(a[0][tope0]+ " "+ a[1][(tope1-1)]);
if(a[1][(tope1-1)]> a[0][tope0]){
temp = a[0][tope0];
a[1][tope1]= temp;
a[0][tope0]= 0;
tope0= tope0 -1;
tope1 = tope1 +1;
System.out.println(a[0][tope0]+ " "+a[1][tope1 - 1] +" "+ a[2][tope2 -1]);
printStatus(a);
}
這是因爲int是一種原始的。應該使用「Integer」代替int –
DUPLICATE:http://stackoverflow.com/questions/11047276/null-for-primitive-data-types –