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, the error:
Exception in thread ' main ' java.lang.UnsupportedOperationException at
java.util.AbstractList.remove ( abstractlist.java:161) at
list. Aslistmethod.main (Aslistmethod.java:12)
The stack bottom list for the wrong chain. Aslistmethod.main (aslistmethod.java:12) says this line of code:
This mistake let me very confused, because I wrote this code before probably looked, arrays.aslist method returns is a ArrayList, I can not remove it? Next, because of a small mistake, the result of the arrays.aslist to Java.util.ArrayList, incredibly assigned value is not successful, I immediately have a blind cat encounter dead Mouse feeling. Then, follow the Arrays.aslist method has been point down, found that this ArrayList is not ArrayList. Arrays.aslist returns the Java.util.Arrays.ArrayList, which is just an internal class of Arrays. Its code is not long, here it is posted directly:
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, it inherits from Abstractlist, and without the Remove method, I find the Abstractlist Remove method:
Public E-Remove (int index) {
throw new unsupportedoperationexception ();
}
This is the source of the error message. Finally, it is known that the Arrays.aslist returned ArrayList does not support remove, in fact, Java.util.Arrays.ArrayList does not support any form of element deletion.
To sum up, the solution to this problem is indeed a bit blind cat touch the smell of dead rats, but, as long as I carefully look at the newspaper error:
At Java.util.AbstractList.remove (abstractlist.java:161)
Calling Java.util.AbstractList's Remove method is different from what I expected, and it clearly tells me the direction.
I hope this article will help you learn Java programming.