【发布时间】:2022-01-04 12:09:18
【问题描述】:
这是我的代码:
private void shrink(){
int length = top+1;
if(length<=MINCAPACITY || top<<2 >= length)
return;
length = length + (top<<1);
if(top < MINCAPACITY) length = MINCAPACITY;
int[] newstack = new int[length];
System.arraycopy(stackRep, 0 , newstack, 0 , top+1);
stackRep = newstack;
}
在我的书中据说如果超过 3/4 为空,则此操作会将数组缩小到一半。谁能向我解释一下这段代码是如何发生的?我怀疑这个操作发生在第一个 if 语句和 length 语句中?
【问题讨论】:
-
那么你不明白代码的哪个确切部分?
标签: java arrays stack bit-shift