in the project List for operating times wrong Java. Lang. UnsupportedOperationException, later found operating List is composed of array transformation, by looking at the source code found problems, and write the test procedure is as follows.
code block:
public class ListTest {
public static void main(String[] args) {
String[] array = {"1","2","3","4","5"};
List<String> list = Arrays.asList(array);
list.add("6");
}
}
execution result:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at com.atguigu.test.ListTest.main(ListTest.java:11)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
The problem with
is the following: the add and remove methods in the List generated by the
call on arrays.aslist () are exception because ArrayList of the inner class of arrays.aslist () is returned instead of java.util.ArrayList. Arrays of inner class ArrayList and Java. Util. ArrayList are inherited AbstractList, remove, add method is the default in AbstractList throw UnsupportedOperationException and don’t make any operation. Java.util.ArrayList overrides these methods, but the ArrayList of Arrays internal class does not, so it will throw an exception. The solution:
public class ListTest {
public static void main(String[] args) {
String[] array = {"1","2","3","4","5"};
List<String> list = Arrays.asList(array);
List arrList = new ArrayList(list);
arrList.add("6");
}
}
div>
Read More:
- java.lang.UnsupportedOperationException: null
- Error in idea compilation: java.lang.OutOfMemoryError Java heap space and java.lang.StackOverflowError
- java.lang.NoSuchMethodError: org.json.JSONObject.put(Java/lang/String; Java/util/Collection;) “209151;”
- build.gradle Warming: cannot be applied to ‘(groovy.lang.Closure)’/'(java.lang.String)’
- Java:java.lang.OutOfMemoryError : GC overhead limit exceeded solution
- [Solved] Sqoop Mysqltohive error: Error: java.lang.RuntimeException: java.lang.RuntimeException…
- Boot / etc/ rc.local What about invalidity? resolvent
- [Solved] Flowable Start Error: ClassCastException: java.time.LocalDateTime cannot be cast to java.lang.String
- Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNot
- Error: java.lang.NumberFormatException : multiple points
- Caused by: java.lang.reflect.InvocationTargetExceptio
- java.lang.reflect.InvocationTargetException
- Java.lang.Character . isdigit() and isletter() methods
- java.lang.IllegalArgumentException: Address 127.0.0.1:5672:5672 seems to contain an unquoted IPv6
- java.lang.IllegalStateException: Could not execute method for android:onClick
- Regular error in UDF java.lang.stackoverflowerror
- java.lang.IllegalArgumentException : urlcoder exception resolution
- Android error java.lang.IllegalStateException : System services not available to Activities before onC
- JDK upgrade 17 project reports an error java.lang.reflect.inaccessibleobjectexception
- Android appears java.lang.NoClassDefFoundError A solution to the error