1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("a"); list.add("ab"); list.add("abc"); list.add("ad"); list.removeIf(s->"a".equals(s)||"c".equals(s)); System.out.println(JSONObject.toJSONString(list)); } 异常操作
List<String> list = Arrays.asList("a", "b", "abcd"); list.removeIf(s -> "a".equals(s) || "c".equals(s)); System.out.println(JSONObject.toJSONString(list)); Exception in thread "main" java.lang.UnsupportedOperationException 原因 @SafeVarargs @SuppressWarnings("varargs") public static <T > List < T > asList(T...a){ return new ArrayList<>(a); }
private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable {
}
AbstractList做任何操作都是异常 public void add ( int index, E element){ throw new UnsupportedOperationException(); }
public E remove ( int index){ throw new UnsupportedOperationException(); } 修改
|