Java. Util. Arrays. The ArrayList (hereinafter: Arrays. The ArrayList) is a Java. Util. Arrays of the private static inner class, implementation of the interface, he inherited the superclass and Java. Almost util. ArrayList (hereinafter: ArrayList (ArrayList) is the same, since it is private, it should be the place we usually pay less attention to. This article attempts to compare one or two differences between them.
Comparison of Usage Scenarios
Construct a List of three strings, ArrayList
List<String> lb = new ArrayList<>(3);
lb.add("a");
lb.add("b");
lb.add("c");
Copy the code
Arrays.ArrayList
List<String> la = Arrays.asList("a", "b", "c"); Public static <T> List<T> asList(T... a) { return new ArrayList<>(a); }Copy the code
They both meet the requirements, the latter looks cleaner than the former, but what’s the difference between the two?
Added comparison of deletion operations
Supported operations,
lb.add("d")
lb.remove("d")
Copy the code
Does not support the operation, it will throw an exception. Java lang. UnsupportedOperationException
la.add("d")
la.remove("d")
Copy the code
ArrayList cannot be added or deleted.
The specific add method
ArrayList doesn’t override its parent’s Add method, so it comes from its parent, AbstractList. Look at AbstractList’s add method
public boolean add(E e) {
add(size(), e);
return true;
}
public void add(int index, E element) {
throw new UnsupportedOperationException();
}
Copy the code
Finally the method called throw UnsupportedOperationException abnormalities. Compare ArrayList
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1); // Increments modCount!!
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = element;
size++;
}
Copy the code
Both of these methods are implemented in an ArrayList, either by expanding and then inserting at the end, or by expanding and moving array elements and then inserting at the specified subscript position.
The specific remove method
The remove(Object) method of ArrayList inherits from AbstractList’s parent class AbstractCollection, where
public boolean remove(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext()) {
if (it.next()==null) {
it.remove();
return true;
}
}
} else {
while (it.hasNext()) {
if (o.equals(it.next())) {
it.remove();
return true;
}
}
}
return false;
}
Copy the code
The iterator is used here to delete. See the comment for this method
If the iterator does not implement the remove method, then the whole method also will throw an UnsupportedOperationException abnormal. AbstractCollection iterator is an abstract method.ArrayList is an AbstractList method.
public Iterator<E> iterator() { return new Itr(); } private class Itr implements Iterator<E> { //... Public void remove() {if (lastRet < 0) throw new IllegalStateException(); checkForComodification(); try { AbstractList.this.remove(lastRet); if (lastRet < cursor) cursor--; lastRet = -1; expectedModCount = modCount; } catch (IndexOutOfBoundsException e) { throw new ConcurrentModificationException(); }}}Copy the code
AbstractList implements an AbstractCollection iterator, and the returned iterator implements a remove iterator, not an annotation. But why is deleting still not allowed? Specifically, the remove method of this iterator,
AbstractList.this.remove(lastRet);
Copy the code
ArrayList doesn’t implement the remove method, but AbstractList does, as follows
public E remove(int index) {
throw new UnsupportedOperationException();
}
Copy the code
So, even if used in AbstractList iterator to delete, but as a result of the Arrays. The ArrayList does not implement the remove and inheritance remove throw UnsupportedOperationException abnormalities, Finally, delete is not allowed on Array. ArrayList.
Note that AbstractList iterators call remove(int) depending on whether the object to remove is an element of an array. If no such element exists, the following code does not raise an exception.
List<String> la = Arrays.asList("a", "b", "c");
la.remove("e");
Copy the code