假设我希望返回tos = tos-2
,而不是如何修改代码?
int pop() {
System.out.print("tos = " +tos+" ");
if (tos<0) {
System.out.println("Stack limit reached .. UNDERFLOW");
return 0;}
else {
return stck[tos--];
}
}
答案 0 :(得分:2)
Java没有一个&#34;递减两个&#34;运算符,因此您必须将其拆分为两行:
tos -= 2;
return stck[tos + 2];
或使用临时值来提高可读性:
tos -= 2;
int returnIndex = tos + 2;
return stck[returnIndex];
答案 1 :(得分:1)
如果要在更改tos
之前返回数组位置(类似于return stck[tos--]
将会执行的操作):
tos-=2;
return stck[tos+2];
否则,
tos-=2;
return stck[tos];
答案 2 :(得分:0)
int pop(){
System.out.print("tos = " +tos+" ");
if (tos<0) {
System.out.println("Stack limit reached .. UNDERFLOW");
return 0;
}
else {
tos = tos-2;
return stck[tos];
}
}
答案 3 :(得分:0)
int pop(){
System.out.print("tos = " +tos+" ");
if (tos<0) {
System.out.println("Stack limit reached .. UNDERFLOW");
return 0;
}
else {
tos -=2;
return stck[tos];
}
}
答案 4 :(得分:0)
停止使用postfix。 :d
int pop() {
int tos = 2;//3
System.out.print("tos = " + tos + " ");
if (tos < 0) {
System.out.println("Stack limit reached .. UNDERFLOW");
return 0;
}
return stck[tos-=2];
}