Java List细节

来源:互联网 发布:网络攻击监测网站 编辑:程序博客网 时间:2024/06/06 03:57

摘自:http://www.cnblogs.com/chenssy/p/3893976.html

1.避免使用基本数据类型数组转换为列表,asList接受的参数是一个泛型的变长参数.
2.asList产生的列表不可操作
asList()方法:

/**     * 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&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");     * </pre>     *     * @param <T> the class of the objects in the array     * @param a the array by which the list will be backed     * @return a list view of the specified array     */    @SafeVarargs    @SuppressWarnings("varargs")    public static <T> List<T> asList(T... a) {        return new ArrayList<>(a);    }

3.subList生成子列表后,不要试图去(结构上的)操作原列表,但可以操作子列表
sublist()方法:

  /**     * Returns a view of the portion of this list between the specified     * {@code fromIndex}, inclusive, and {@code toIndex}, exclusive.  (If     * {@code fromIndex} and {@code toIndex} are equal, the returned list is     * empty.)  The returned list is backed by this list, so non-structural     * changes in the returned list are reflected in this list, and vice-versa.     * The returned list supports all of the optional list operations.     *     * <p>This method eliminates the need for explicit range operations (of     * the sort that commonly exist for arrays).  Any operation that expects     * a list can be used as a range operation by passing a subList view     * instead of a whole list.  For example, the following idiom     * removes a range of elements from a list:     * <pre>     *      list.subList(from, to).clear();     * </pre>     * Similar idioms may be constructed for {@link #indexOf(Object)} and     * {@link #lastIndexOf(Object)}, and all of the algorithms in the     * {@link Collections} class can be applied to a subList.     *     * <p>The semantics of the list returned by this method become undefined if     * the backing list (i.e., this list) is <i>structurally modified</i> in     * any way other than via the returned list.  (Structural modifications are     * those that change the size of this list, or otherwise perturb it in such     * a fashion that iterations in progress may yield incorrect results.)     *     * @throws IndexOutOfBoundsException {@inheritDoc}     * @throws IllegalArgumentException {@inheritDoc}     */    public List<E> subList(int fromIndex, int toIndex) {        subListRangeCheck(fromIndex, toIndex, size);        return new SubList(this, 0, fromIndex, toIndex);    }
```public static void main(String[] args) {                                List<Integer> list1 = new ArrayList<Integer>();               list1.add(1);            list1.add(2);       List<Integer> list2 = new ArrayList<Integer>(list1);                     List<Integer> list3 = list1.subList(0,       list1.size());    list3.add(3);                    System.out.println("list1 == list2:" +list1.equals(list2));//false    System.out.println("list1 == list3:" +list1.equals(list3));//true}