未获支持的操作都是由固定尺寸的数据结构支持的容器产生的。
    1,Arrays.asList 只支持只读操作
    2,Collections包装的容器

    1. public class UnSupported {
    2. static void test(String msg, List<String> list){
    3. System.out.println("----"+msg+"---");
    4. Collection<String> c = list;
    5. Collection<String> subList = list.subList(1,8);
    6. Collection<String> c2 = new ArrayList<>(subList);
    7. try { c.retainAll(c2); } catch(Exception e) {
    8. System.out.println("retainAll(): " + e);
    9. }
    10. try { c.removeAll(c2); } catch(Exception e) {
    11. System.out.println("removeAll(): " + e);
    12. }
    13. try { c.clear(); } catch(Exception e) {
    14. System.out.println("clear(): " + e);
    15. }
    16. try { c.add("X"); } catch(Exception e) {
    17. System.out.println("add(): " + e);
    18. }
    19. try { c.addAll(c2); } catch(Exception e) {
    20. System.out.println("addAll(): " + e);
    21. }
    22. try { c.remove("C"); } catch(Exception e) {
    23. System.out.println("remove(): " + e);
    24. }
    25. // The List.set() method modifies the value but
    26. // doesn't change the size of the data structure:
    27. try {
    28. list.set(0, "X");
    29. } catch(Exception e) {
    30. System.out.println("List.set(): " + e);
    31. }
    32. }
    33. public static void main(String[] args) {
    34. List<String> list = Arrays.asList("A B C D E F G H I J K L".split(" "));
    35. //经过ArrayList包裹的类 支持全部的操作
    36. ArrayList<String> arrayList = new ArrayList<>(list);
    37. //未包装的list 仅支持只读和修改的操作
    38. //test("Arrays.asList()", list);
    39. //System.out.println(list);
    40. List<String> unmodifiableList = Collections.unmodifiableList(new ArrayList<String>(list));
    41. //仅支持只读操作,修改也不支持
    42. test("unmodifiableList()", unmodifiableList);
    43. System.out.println(unmodifiableList);
    44. }
    45. }