繁体   English   中英

此代码中Integer.Min_value的用途是什么?

[英]What is the use of Integer.Min_value in this code?

此代码中Integer.Min_value的用途是什么? 我知道它是对垃圾回收的取消引用S [top],但需要更多了解

public int pop() throws Exception {

    int data;
    if (isEmpty())
        throw new Exception("Stack is empty.");
    data = S[top];
    S[top--] = Integer.MIN_VALUE; 
    return data;
} 

假设您有一个定义为如下数组的堆栈:

S = [ 1, 2, 3, 4, 5 ]

堆栈是后进先出。 top变量应始终是“ last in”堆栈项的索引,在这种情况下, top = 4因为in的最后一个数字为5且索引为4

这是将S[top]的内容分配给data ,然后将Integer.MIN_VALUE分配给S[top]以“清除”它,然后将top减1。

这行: S[top--] = Integer.MIN_VALUE; 可以重写为以下内容:

S[top] = Integer.MIN_VALUE; top = top - 1;

我认为最终目标是清除堆栈中的旧值并为它们提供默认值,而不必调整数组的大小。

执行pop方法后,结果为: S: [ 1, 2, 3, 4, -2147483648 ]top: 3 ,堆栈中的“ last in”项将为4

希望这是有道理的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM