数组转List

用Arrays.asList可以直接将数组转化为List

        List list = Arrays.asList("check 01","test 01","test 02","check 02","test 03");
        System.out.println(list);

// 返回值
[check 01, test 01, test 02, check 02, test 03]

但是如果操作list进行remove,就会报错了

		List list = Arrays.asList("check 01","test 01","test 02","check 02","test 03");
		System.out.println(list);
		nameList.remove("check 01");
		System.out.println(list);


// 报错
[check 01, test 01, test 02, check 02, test 03]
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.util.AbstractList.remove(AbstractList.java:161)
	at java.util.AbstractList$Itr.remove(AbstractList.java:374)
	at java.util.AbstractCollection.remove(AbstractCollection.java:293)
	at com.paralworld.util.TestUtil.main(TestUtil.java:55)

这个时候,需要先转换一遍list

		List list = Arrays.asList("check 01","test 01","test 02","check 02","test 03");
		System.out.println(list);
		List nameList = new ArrayList<>(list);
		nameList.remove("check 01");
		System.out.println(list);
		System.out.println(nameList);


// 返回值
[check 01, test 01, test 02, check 02, test 03]
[check 01, test 01, test 02, check 02, test 03]
[test 01, test 02, check 02, test 03]

你可能感兴趣的:(list,windows,linux)