List<integer> numbers= arrays.aslist (new integer[] {A-I}); Numbers.add (3);
Running this piece of code will throw
java.lang.UnsupportedOperationException at Java.util.AbstractList.add (Unknown Source) at Java.util.AbstractList . Add (Unknown Source) at Com.liqiang.task.MailTest.testMail (Mailtest.java:39) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Unknown source) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Unknown source) at Java.lang.reflect.Method.invo Ke (Unknown Source) at org.junit.runners.model.frameworkmethod$1.runReflectiveCall (frameworkmethod.java:45) at Org.junit.internal.runners.model.ReflectiveCallable.run (Reflectivecallable.java:15) at org.junit.runners.model.FrameworkMethod.invokeExplosively (Frameworkmethod.java:42) at Org.junit.internal.runners.statements.InvokeMethod.evaluate (Invokemethod.java:20) at Org.junit.internal.runners.statements.RunBefores.evaluate (Runbefores.java:28) at org.activiti.engine.test.activitirule$1.evaluate (activitirule.java:126) at Org.junit.rules.RunRules.evaluate (Runrules.java:18) at Org.junit.runners.ParentRunner.runLeaf (Parentrunner.java:263) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (Blockjunit4classrunner.java:68) at Org.junit.runners.BlockJUnit4ClassRunner.runChild (Blockjunit4classrunner.java:47) at org.junit.runners.parentrunner$3.run (parentrunner.java:231) at org.junit.runners.parentrunner$1.schedule (parentrunner.java:60) at Org.junit.runners.ParentRunner.runChildren (Parentrunner.java:229) at org.junit.runners.parentrunner.access$(parentrunner.java:50) at org.junit.runners.parentrunner$2.evaluate (parentrunner.java:222) at Org.junit.internal.runners.statements.RunBefores.evaluate (Runbefores.java:28) at Org.junit.internal.runners.statements.RunAfters.evaluate (Runafters.java:30) at Org.junit.runners.ParentRunner.run (Parentrunner.java:300) at Org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run (Junit4testreference.java:86) at Org.eclipse.jdt.internal.junit.runner.TestExecution.run (Testexecution.java:38) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (Remotetestrunner.java:538) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests (Remotetestrunner.java:760) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run (Remotetestrunner.java:460) at Org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main (Remotetestrunner.java:206)
The exception,
View Arrays.as Source code
public static <T> list<t > Aslist (T ... a) { return new arraylist<> (a); /** * @serial include */ private static class ArrayList <E> extends abstractlist<e> Implements Randomaccess, java.io.Serializable {
....
}
The new ArrayList is created inside the ArrayList instead of the Java.util.Arrays package under the ArrayList
Find internal ArrayList and no Add method click on parent class to view
PublicE Set (intindex, E Element) { Throw Newunsupportedoperationexception (); } /** * {@inheritDoc} * * <p>this implementation always throws an * {@codeunsupportedoperationexception}. * * @throwsunsupportedoperationexception {@inheritDoc} * @throwsclasscastexception {@inheritDoc} * @throwsNullPointerException {@inheritDoc} * @throwsIllegalArgumentException {@inheritDoc} * @throwsindexoutofboundsexception {@inheritDoc} */ Public voidAddintindex, E Element) { Throw Newunsupportedoperationexception (); } /** * {@inheritDoc} * * <p>this implementation always throws an * {@codeunsupportedoperationexception}. * * @throwsunsupportedoperationexception {@inheritDoc} * @throwsindexoutofboundsexception {@inheritDoc} */ PublicE Remove (intindex) { Throw Newunsupportedoperationexception (); }
The method of modifying a collection element, such as the add remove, implemented by the parent class, throws an exception throw new Unsupportedoperationexception ()
So the collection that arraylist.as produces cannot be modified.
The array.aslist of Java traps