import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; public class ArraysAsListDemo { public static void main(String[] args) { List<String> source1 = new ArrayList<String>(Arrays.asList("1", "2", "3")); ArrayList<String> source2 = new ArrayList<String>(Arrays.asList("5", "62", "3")); source2.removeAll(source1); System.out.println(source2); // Arrays.asList("1", "2", "3").remove("1");//会报错。因为Arrays$ArrayList中没有实现remove方法 System.out.println(source1.containsAll(source2)); HashSet<String> total = new HashSet<String>(); total.addAll(source1); total.addAll(source2); System.out.println(total.size() > (source1.size() + source2.size())); System.out.println(total.size() < (source1.size() + source2.size())); } }
// Misc /** * Returns a fixed-size list backed by the specified array. (Changes to * the returned list "write through" to the array.) This method acts * as bridge between array-based and collection-based APIs, in * combination with {@link Collection#toArray}. The returned list is * serializable and implements {@link RandomAccess}. * * <p>This method also provides a convenient way to create a fixed-size * list initialized to contain several elements: * <pre> * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); * </pre> * * @param a the array by which the list will be backed * @return a list view of the specified array */ public static <T> List<T> asList(T... a) { return new ArrayList<T>(a); } /** * @serial include */ private static class ArrayList<E> extends AbstractList<E> implements RandomAccess, java.io.Serializable { private static final long serialVersionUID = -2764017481108945198L; private final E[] a; ArrayList(E[] array) { if (array==null) throw new NullPointerException(); a = array; }
时间: 2024-10-13 07:27:21