ArrayList.add() and aslist().add() problem
using aslist().add() report an error: UnsupportedOperationException
Cause and analysis
asList() is using the internal class ArrayList of Arrays, this internal class does not override the add() method, so the add method used here is the parent class abstract class of the ArrayList class, the add() method here by default is to throw an exception: UnsupportedOperationException
Internal class of arrays
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);
}
@Override
public int size() {
return a.length;
}
@Override
public Object[] toArray() {
return a.clone();
}
@Override
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
int size = size();
if (a.length < size)
return Arrays.copyOf(this.a, size,
(Class<?extends T[]>) a.getClass());
System.arraycopy(this.a, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
@Override
public E get(int index) {
return a[index];
}
@Override
public E set(int index, E element) {
E oldValue = a[index];
a[index] = element;
return oldValue;
}
@Override
public int indexOf(Object o) {
E[] a = this.a;
if (o == null) {
for (int i = 0; i < a.length; i++)
if (a[i] == null)
return i;
} else {
for (int i = 0; i < a.length; i++)
if (o.equals(a[i]))
return i;
}
return -1;
}
@Override
public boolean contains(Object o) {
return indexOf(o) != -1;
}
@Override
public Spliterator<E> spliterator() {
return Spliterators.spliterator(a, Spliterator.ORDERED);
}
@Override
public void forEach(Consumer<?super E> action) {
Objects.requireNonNull(action);
for (E e : a) {
action.accept(e);
}
}
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
@Override
public void sort(Comparator<?super E> c) {
Arrays.sort(a, c);
}
}
Add method of ArrayList() class
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
Add method of AbstractList class
Aslist() uses this method
public void add(int index, E element) {
throw new UnsupportedOperationException();//This is where the exception is thrown
}
Read More:
- [Solved] Error getting generated key or setting result to parameter object. UnsupportedOperationException
- Elasticsearch Startup Error: unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp
- [Solved] Springboot uses redis to add LocaldateTime Java 8 error
- [Solved] Springcloud Add Eureka to Startup Error
- JAVA: How to Solve Foreach Loop Remove/add Element Error
- [Solved] Mybatis add dependencies Error: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration.
- Tensorflow Error raise RuntimeError(‘The Session graph is empty. Add operations to the ‘ RuntimeError:
- [Solved] IDEA Add maven Project Error: Error:(3,21)java: Package javax.servletdoes not exist
- How to Solve Flick operate Error: not serialized
- JAVA 8: How to Convert List to Map
- Redis: How to Implementate LRU Caching Mechanism Manually
- C language: Implementation of dynamic array initialization, insertion, traversal, deletion and destruction
- List: How to de-duplication according to an attribute of an object
- [Solved] Method threw ‘java.lang.StackOverflowError‘ exception. Cannot evaluate
- H2 memory database Oracle mode page error: rg.springframework.dao.InvalidDataAccessResourceUsageException: could not prepar
- Comparison between ArrayList and HashMap
- [Solved] Java.lang.IllegalStateException: getReader() has already been called for this request
- [Solved] Multithreading uses jsch to obtain a session for connection error: session.connect: java.net.socketexception: connection reset
- Java Error: No enclosing instance of type Main is accessible. Must qualify the allocation with an encl
- The thread implementation of timer in Java