java.util.ArrayList 和 Arrays.asList返回的ArrayLis【新人菜鸟,不对的请帮忙改正】

 

前提:

使用迭代器删除List中的元素,发现删除时报错。

 

代码:

   List list = Arrays.asList("aaa","aaab","ccc","ccca","efgr");
    Iterator iterator = list.iterator();
 while (iterator.hasNext()){

     String s = iterator.next();
     if(s.contains("a")){
         iterator.remove();
     }
     System.out.println("delete前"+s);
 }
    System.out.println("delete后"+list1);
}

错误:ava.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:161)
    at java.util.AbstractList$Itr.remove(AbstractList.java:374)
    at com.example.demo.service.TestService.test10(TestService.java:180)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)

原因:

asList返回的是Arrays下的ArrayList 并不是Util下的ArrayList 没有实现romove的方法 因此不能使用迭代器去删除

 

修改:

   //asList返回的是Arrays下的ArrayList 并不是Util下的ArrayList 没有实现romove的方法 因此不能使用迭代器去删除
   List list = Arrays.asList("aaa","aaab","ccc","ccca","efgr");
   //将返回的list重新包装
   List list1 = new ArrayList<>(list);
   Iterator iterator = list1.iterator();
while (iterator.hasNext()){

    String s = iterator.next();
    if(s.contains("a")){
        iterator.remove();
    }
    System.out.println("delete前"+s);
}
   System.out.println("delete后"+list1);

 

结果:

delete前aaa
delete前aaab
delete前ccc
delete前ccca
delete前efgr
delete后[ccc, efgr]

你可能感兴趣的:(java.util.ArrayList 和 Arrays.asList返回的ArrayLis【新人菜鸟,不对的请帮忙改正】)