package com; import java.util.Arrays; import java.util.List; public class ArrayAsListTest { public static void main(String[] args) { String[] arr = new String[]{"a","b","c"}; List错误原理arrayList = Arrays.asList(arr); arrayList.add("d"); } }
此ArrayList非彼ArrayList , java.util.Arrays.ArrayList.ArrayList 它是array类的一个内部类
package com; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayAsListTest { public static void main(String[] args) { // String[] arr = new String[]{"a","b","c"}; // ListarrayList = Arrays.asList(arr); // arrayList.add("d"); String[] arr = new String[]{"a","b","c"}; List arrayList = new ArrayList<>(Arrays.asList(arr)); arrayList.add("d"); System.out.println(arrayList.size()); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)