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<String> 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}
阅读全文
0 0
- Java List细节
- java 细节 split List 中的equal
- java 细节 有关list的赋值
- 关于Java中的List容器的remove注意细节
- C#:List细节
- java细节
- java细节
- java细节
- java细节
- java 细节
- Java细节
- java 细节
- Java细节
- java细节
- Java细节
- java 细节
- java细节
- C# List<>.Add一个细节
- Android 微信支付和支付宝支付 快速接入
- [转]Windows实用工具Dism++ 10.1.1000.50
- 动态规划算法思想解决找零钱问题
- Eclipse实用操作
- DPDK-实战之rxtx_callbacks(回调函数)
- Java List细节
- 获取FTP服务器上以“ipva”“ap”开头,和其他文件的数目
- vue.js初学1
- c++11多线程的创建和unique_lock<mutex>的使用
- Logger日志库的基本使用
- layer.closeAll()无法关闭弹窗的解决办法之一
- [51nod-1563]坐标轴上的最大团
- hdu 6180 Schedule 贪心
- zookeeper单机单独实例安装-windows