import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SimpleArrays {
public static void main(String[] args) {
String[] string = new String[] { "111", "222", "333" };
List<String> list =Arrays.asList(string);;
list.add("444");
System.out.println(list);
}
import java.util.Arrays;
import java.util.List;
public class SimpleArrays {
public static void main(String[] args) {
String[] string = new String[] { "111", "222", "333" };
List<String> list =Arrays.asList(string);;
list.add("444");
System.out.println(list);
}
}
这样必然会报错
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.AbstractList.add(AbstractList.java:148)
at java.util.AbstractList.add(AbstractList.java:108)
at SimpleArrays.main(SimpleArrays.java:16)
正确的写法应该这样
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SimpleArrays {
public static void main(String[] args) {
String[] string = new String[] { "111", "222", "333" };
List<String> list =new ArrayList<String>(Arrays.asList(string));
list.add("444");
System.out.println(list);
}
}
完美运行
[111, 222, 333, 444]