简体   繁体   English

我的ArrayList会发生什么

[英]what will happen to my ArrayList

I am trying to understand Java. 我想了解Java。

Let's assume I have an ArrayList of size 50 and pre-populated with some names. 假设我有一个大小为50的ArrayList ,并预先填充了一些名称。

Let's assume I remove 3rd and 4th element from the array list. 假设我从数组列表中删除第3个和第4个元素。 What will happen to my array list? 我的数组列表会发生什么? Will it get rearranged? 会重新排列吗? Will it return null if I try to access the now-deleted 3rd and 4th elements? 如果我尝试访问现在删除的第3和第4个元素,它会返回null吗?

No, the elements after the one you are going to delete will be shifted to the left (expensive operation), so you won't have any hole. 不,你要删除的元素之后的元素将被移到左边(昂贵的操作),所以你不会有任何漏洞。

As as side note: if you remove the 3rd element then the 5th element will be shifted to the left, so if afterwards you remove the 4th, you are instead removing the fifth of the starting collection. 如旁注:如果删除第3个元素,则第5个元素将向左移动,因此如果之后删除第4个元素,则取而代之的是删除第5个元素。 To remove two consecutive elements you should provide the same index twice. 要删除两个连续元素,您应该提供两次相同的索引。

They will be rearranged and shifted. 他们将被重新安排和转移。

If you want them to return null instead, just set those elements you want to remove to null explicitly rather than removing them. 如果您希望它们返回null ,只需将要删除的元素显式设置为null,而不是删除它们。

Why didn't you try it yourself? 你为什么不亲自尝试一下?

List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
list.add("F");
list.add("G");

for(int i = 0; i < list.size(); i++) System.out.println("index " + i + ": " + list.get(i));

System.out.println();
list.remove(0); // remove "A"

for(int i = 0; i < list.size(); i++) System.out.println("index " + i + ": " + list.get(i));

OUTPUT: OUTPUT:

index 0: A
index 1: B
index 2: C
index 3: D
index 4: E
index 5: F
index 6: G

index 0: B
index 1: C
index 2: D
index 3: E
index 4: F
index 5: G

You actually have both options available: 您实际上有两种选择:

final List<Character> x = new ArrayList<Character>(asList('a', 'b', 'c', 'd'));
x.set(1, null); // removes an element without shifting
x.remove(0);    // removes an element with shifting
System.out.println(x);

Prints 打印

[null, c, d]

数组列表元素将重新排列

An ArrayList is a consecutive list of items that can be referenced by an index. ArrayList是索引可以引用的项的连续列表。 So when you delete an item, all following items will be shifted. 因此,当您删除项目时,将移动以下所有项目。

The elements will be shifted. 元素将被转移。

See the javadoc for ArrayList remove: 请参阅javadoc for ArrayList remove:

java.util.ArrayList
public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
Specified by:
remove in interface List
Overrides:
remove in class AbstractList
Parameters:
index - the index of the element to be removed
Returns:
the element that was removed from the list
Throws:
IndexOutOfBoundsException -

根据remove方法javadoc,剩下的条目将向后移动,因此没有间隙。

Usually you use java collections as a dynamic storage, without defining the size. 通常,您使用java集合作为动态存储,而不定义大小。

List<String> list = new ArrayList<String>(); //dynamic

For static pre-defined collections you use java arrays . 对于静态预定义集合,您使用java数组

String[] array = new String[50]; //static

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

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