First, give a piece of code:
public class AslistMethod { public static void main(String[] args) { String sentence = "i love you"; List<String> words = Arrays.asList(sentence.split(" ")); for (int i = 0; i < words.size(); ) { if (words.get(i).equals("love")) { words.remove(i); continue; } i++; } System.out.println(words); }Then, run this code, and finally, an error was reported:
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(AbstractList.java:161) at list.AslistMethod.main(AslistMethod.java:12)
The bottom of the error chain list.AslistMethod.main(AslistMethod.java:12) is about this line of code:
words.remove(i);
This error made me confused because I roughly looked at this code before writing this code. The Arrays.asList method returns an ArrayList. Why can't I remove it? Next, due to a small mistake, the result of Arrays.asList was assigned to java.util.ArrayList, but the assignment failed. I immediately felt like a blind cat hit a dead mouse. Then, follow the Arrays.asList method and find that this ArrayList is not the same ArrayList. Arrays.asList returns java.util.Arrays.ArrayList, which is just an inner class of Arrays. Its code is not long, so I posted it directly here:
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) { 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); } } From the source code, it inherits from AbstractList and there is no remove method, I found the remove method of AbstractList:
public E remove(int index) { throw new UnsupportedOperationException(); } This is where the error message comes from. Finally, we know that the ArrayList returned by Arrays.asList does not support remove. In fact, java.util.Arrays.ArrayList does not support any form of element removal.
To sum up, the solution to this problem is indeed a bit like a blind cat hitting a dead mouse, but as long as I look carefully at the error reported:
at java.util.AbstractList.remove(AbstractList.java:161)
Calling the remove method of java.util.AbstractList is different from what I expected, and it can tell me the direction clearly.
I hope this article will be helpful to everyone to learn Java programming.