java集合中,list列表應該是我們最常使用的,它有兩種常見的實現類:ArrayList和LinkedList。ArrayList底層是數組,查找比較方便;LinkedList底層是鏈表,更適合做新增和刪除。但實際開發中,我們也會遇到使用ArrayList需要刪除列表元素的時候。雖然ArrayList類已經提供了remove方法,不過其中有潛在的坑,下面將介紹remove方法的三種錯誤用法以及六種正確用法。
1、錯誤用法
1.1、for循環中使用remove(int index),列表從前往后遍歷
首先看一下ArrayList.remove(int index)的源碼,讀代碼前先看方法注釋:移除列表指定位置的一個元素,將該元素后面的元素們往左移動一位。返回被移除的元素。
源代碼也比較好理解,ArrayList底層是數組,size是數組長度大小,index是數組索引坐標,modCount是被修改次數的計數器,oldValue就是被移除索引的元素對象,numMoved是需要移動的元素數量,如果numMoved大于0,則執行一個數組拷貝(實質是被移除元素后面的元素都向前移動一位)。然后數組長度size減少1,列表最后一位元素置為空。最后將被移除的元素對象返回。
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts one from their
* indices).
*
* @param index the index of the element to be removed
* @return the element that was removed from the list
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
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; // clear to let GC do its work
return oldValue;
}
如果在for循環中調用了多次ArrayList.remove(),那代碼執行結果是不準確的,因為每次每次調用remove函數,ArrayList列表都會改變數組長度,被移除元素后面的元素位置都會發生變化。比如下面這個例子,本來是想把列表中奇數位置的元素都移除,但最終得到的結果是[2,3,5]。
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 3L, 4L, 5L));
for (int i = 0; i < list.size(); i++) {
if (i % 2 == 0) {
list.remove(i);
}
}
//最終得到[2,3,5]
1.2、直接使用list.remove(Object o)
ArrayList.remove(Object o)源碼的邏輯和ArrayList.remove(int index)大致相同:列表索引坐標從小到大循環遍歷,若列表中存在與入參對象相等的元素,則把該元素移除,后面的元素都往左移動一位,返回true,若不存在與入參相等的元素,返回false。
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
}
如果直接對list調用了該方法,代碼結果可能會不準確。例子如下:這段代碼本想移除列表中全部值為2的元素,結果并沒有成功。
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 2L, 4L, 5L));
list.remove(2L);
//最終得到[1,2,4,5]
1.3、Arrays.asList()之后使用remove()
為啥使用了Arrays.asList()之后使用remove是錯誤用法,我們看一下asList()的源碼就能知道了。Arrays.asList()返回的是一個指定數組長度的列表,所以不能做Add、Remove等操作。至于為啥是返回的是固定長度的,看下面源碼,asList()函數中調用的new ArrayList<>()并不是我們常用的ArrayList類,而是一個Arrays的內部類,也叫ArrayList,而且這個內部類也是基于數組實現的,但它有一個明顯的關鍵字修飾,那就是final。都用final修飾了,那是肯定不能再對它進行add/remove操作的。如果非要在Arrays.asList之后使用remove,正確用法參見2.5。
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
private static final long serialVersionUID = -2764017481108945198L;
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
}
2、正確用法
2.1、直接使用removeIf()
使用removeIf()這個方法前,我是有點害怕的,畢竟前面兩個remove方法都不能直接使用。于是小心翼翼的看了removeIf函數的方法。確認過源碼,是我想要的方法!
源碼如下:removeIf()的入參是一個過濾條件,用來判斷需要移除的元素是否滿足條件。方法中設置了一個removeSet,把滿足條件的元素索引坐標都放入removeSet,然后統一對removeSet中的索引進行移除。源碼相對復雜的是BitSet模型,源碼這里不再貼了。
public boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
// figure out which elements are to be removed
// any exception thrown from the filter predicate at this stage
// will leave the collection unmodified
int removeCount = 0;
final BitSet removeSet = new BitSet(size);
final int expectedModCount = modCount;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
@SuppressWarnings("unchecked")
final E element = (E) elementData[i];
if (filter.test(element)) {
removeSet.set(i);
removeCount++;
}
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
// shift surviving elements left over the spaces left by removed elements
final boolean anyToRemove = removeCount > 0;
if (anyToRemove) {
final int newSize = size - removeCount;
for (int i=0, j=0; (i < size) && (j < newSize); i++, j++) {
i = removeSet.nextClearBit(i);
elementData[j] = elementData[i];
}
for (int k=newSize; k < size; k++) {
elementData[k] = null; // Let gc do its work
}
this.size = newSize;
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
return anyToRemove;
}
removeIf()的使用方法如下所示(jdk8),結果滿足預期。
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 2L, 4L, 5L)); list.removeIf(val -> val == 2L); //結果得到[1L,4L,5L]
2.2、在for循環之后使用removeAll(Collection<?> c)
這種方法思路是for循環內使用一個集合存放所有滿足移除條件的元素,for循環結束后直接使用removeAll方法進行移除。removeAll源碼如下,還是比較好理解的:定義了兩個數組指針r和w,初始都指向列表第一個元素。循環遍歷列表,r指向當前元素,若當前元素沒有滿足移除條件,將數組[r]元素賦值給數組[w],w指針向后移動一位。這樣就完成了整個數組中,沒有被移除的元素向前移動。遍歷完列表后,將w后面的元素都置空,并減少數組長度。至此完成removeAll移除操作。
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
// Preserve behavioral compatibility with AbstractCollection,
// even if c.contains() throws.
if (r != size) {
System.arraycopy(elementData, r,
elementData, w,
size - r);
w += size - r;
}
if (w != size) {
// clear to let GC do its work
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
正確使用方式如下:
List<Long> removeList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (i % 2 == 0) {
removeList.add(list.get(i));
}
}
list.removeAll(removeList);
2.3、list轉為迭代器Iterator的方式
迭代器就是一個鏈表,直接使用remove操作不會出現問題。
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
if (it.next() % 2 == 0)
it.remove();
}
2.4、for循環中使用remove(int index), 列表從后往前遍歷
前面1.1也是for循環,為啥從后往前遍歷就是正確的呢。因為每次調用remove(int index),index后面的元素會往前移動,如果是從后往前遍歷,index后面的元素發生移動,跟index前面的元素無關,我們循環只去和前面的元素做判斷,因此就沒有影響。
for (int i = list.size() - 1; i >= 0; i--) {
if (list.get(i).longValue() == 2) {
list.remove(i);
}
}
2.5、Arrays.asList()之后使用remove()
Arrays.asList()之后需要進行add/remove操作,可以使用下面這種方式:
String[] arr = new String[3]; List list = new ArrayList(Arrays.asList(arr));
2.6、使用while循環
使用while循環,刪除了元素,索引便不+1,在沒刪除元素時索引+1
int i=0;
while (i<list.size()) {
if (i % 2 == 0) {
list.remove(i);
}else {
i++;
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。