【发布时间】:2019-02-24 00:36:57
【问题描述】:
我不确定这种方法是如何工作的。我的理解是我们创建了一个临时数组,其大小是theItems.length 的两倍(theItems 是另一个数组)。之后,我们将项目复制到 temp 数组,最后我们编写 theItems = temp; (我不确定为什么以及会发生什么)(这是否意味着 theItems 的大小也翻了一番?)。我们不能在不使用 temp 的情况下将 theItems 的大小加倍吗?
private void resize() {
String[] temp = new String[theItems.length*2];
for (int i=0 ; i < noOfItems ; i++){
temp[i]=theItems[i];
}
theItems=temp;
}
【问题讨论】:
-
theItems = Arrays.copyOf(theItems, 2*theItems.length);