![](/img/trans.png)
[英]How remove(Object obj) method in List in java is implemented?
[英]How is the remove method for ArrayList in Java implemented?
假设我正在尝试从头开始实现Java的ArrayList
。 我知道我会使用称为数组的数据结构,必须在需要时扩展其容量,并跟踪大小(数组中的项目数)和数据(数组的内容)之类的东西。
由于这种尺寸的实例变量的,当我们从去除的东西ArrayList
,例如从除去4 2, 3, 4, 5
,我们将留下2, 3, 5, [empty space]
,正确的吗? 据我所知,我们将设置等于下一个元素的值,因此实际上不缩小数组的大小,仅在需要时才对其进行扩展。
如果我的问题不清楚,我只是在寻找ArrayList
的remove(Object element)
实现。
我希望我有一个图表来解释。 如果还有其他需要,请询问。 谢谢。
当我为2016年的GATE(工程学能力倾向测试)考试做准备时,我对C lang也有类似的疑问。
好吧,从那时起,我提高了编码技能,现在我用Java编写代码。
根据列表 API
删除此列表中指定位置的元素(可选操作)。 将所有后续元素向左移动(从其索引中减去一个)。 返回从列表中删除的元素。
ArrayList
类是List
接口的基于数组的实现。 具体来说, ArrayList
所有元素都存储在Java数组中。
Arraylist.remove(E)
的实现如下:
public E remove(int index) {
rangeCheck(index);
modCount++;
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // Let gc(garbage collector) do its work
return oldValue;
}
其中elementData
是后备元素数组。
换句话说,与其说是转移,不如说是转移元素,减去缺失的元素。
给定的elementData
具有4个元素
[ 2 | 3 | 4 | 5 ]
现在删除索引2的元素
导致[2 | 3 | 5 | 空值 ]
ArrayList.get(2)
将导致5
这非常简单,并解释了为什么计划删除不最后的元素时不使用ArrayList
保留。
ArrayList
有2个部分: 内部数组 (默认大小为10- ArrayList.DEFAULT_CAPACITY = 10
)和大小值(默认值为0
)。
注意1.当您可以预测ArrayList
最大大小时,请将内部数组初始化为该大小。
List<String> list1 = new ArrayList<>(); // size = 0, internal array Object[] elementData = new Object[10];
List<String> list2 = new ArrayList<>(66); // size = 0, internal array Object[] elementData = new Object[66];
在最后添加元素
size == elementData.length
>创建更大的新elementData
并将所有现有数据复制到此新数组 size++
(数组末尾有许多null
元素) 从末端删除元素
size > 0
) size--
从开头或中间删除元素
size--
如您所见, ArrayList
大小和内部数组elementData
长度之间没有直接关联。
演示:
List<String> list = new ArrayList<>(4); // [ null | null | null | null ], size = 0
list.add("2"); // [ "2" | null | null | null ], size = 1
list.add("3"); // [ "2" | "3" | null | null ], size = 2
list.add("4"); // [ "2" | "3" | "4" | null ], size = 3
list.add("5"); // [ "2" | "3" | "4" | "5" ], size = 4
list.remove(3); // [ "2" | "3" | "5" | null ], size = 3
list.remove(1); // [ "3" | "5" | null | null ], size = 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.