我在java collection util中面临一些问题,基本上我通过打破这些步骤在arrayList上使用了removeall ()但它抛出java.lang.UnsupportedOperationException,当我在单行中执行它时,它按预期正常工作.所以当我分几个步骤打破它时,我不明白是什么问题.代码是
public class Test4 { public static void main(String args[]){ String unInstall="com.mobikwik_new,com.cleanmaster.mguard,com.htc.flashlight,com.mobilemotion.dubsmash"; String install="com.mobikwik_new,com.cleanmaster.mguard,com.htc.flashlight"; ListinstallList = new ArrayList (); List unInstallList = new ArrayList (); String inL[] = install.split(","); String UnInL[] = unInstall.split(","); installList = Arrays.asList(inL); unInstallList = Arrays.asList(UnInL); unInstallList.remove(installList); //List installList = new ArrayList<>(Arrays.asList(install.split(","))); //List unInstallList = new ArrayList<>(Arrays.asList(unInstall.split(","))); unInstallList.removeAll(installList); System.out.println("unInstall : "+unInstallList); } }
注意:当我只使用注释行而不是上述所有步骤时,它的工作正常
投掷的例外是 -
Exception in thread "main" java.lang.UnsupportedOperationException at java.util.AbstractList.remove(Unknown Source) at java.util.AbstractList$Itr.remove(Unknown Source) at java.util.AbstractCollection.removeAll(Unknown Source) at Test4.main(Test4.java:21)
谢谢 !
Arrays.asList
返回一个固定大小的列表,因为它由作为参数给出的数组支持.返回的列表不支持该remove
操作,如您所看到的错误消息所示.
如果要从列表中删除某些内容,请将返回的列表包装在ArrayList
:中new ArrayList<>(Arrays.asList(inL));
.