List接口案例
来源:互联网 发布:软件测试平台搭建 编辑:程序博客网 时间:2024/04/30 09:35
package com.neutron.collection.list;import java.util.ArrayList;import java.util.Iterator;import java.util.List;/** * Collection接口的子类List,和Set * List是有序的,并且允许元素重复,因为List等层实现是数字,存在索引,index * Set是无需的,并且不允许元素重复 * * 相对于Collection共性的方法,List特有的方法都是和索引有关系的内容。 * 增加: add(index,element), addAll(index, collection) * 删除: remove(index) * 修改: set(index, element) * 查询: get(index), subList(from,to), listIterator() * @author zhanght * */public class ListDemo {public static void main(String[] args) {List arr = new ArrayList();// 添加元素arr.add(0, "123");arr.add(1, 345);arr.add(2, "hello");List insert = new ArrayList();insert.add(0, "insert0");insert.add(1, "insert1");// 指定位置添加元素arr.add(3, insert);arr.addAll(4, insert);System.out.println(arr);//[123, 345, hello, [insert0, insert1], insert0, insert1]// add: insert element; addAll: insert all elements// 删除索引位置元素arr.remove(3);System.out.println(arr);// [123, 345, hello, insert0, insert1]// 修改元素arr.set(3, "insert3");System.out.println(arr);// [123, 345, hello, insert3, insert1]// we can see, set means edit, not addObject obj = arr.get(3);System.out.println("insert3".equals(obj));System.out.println(arr);// [123, 345, hello, insert3, insert1]List subList = arr.subList(1, 4);System.out.println("截取数据subList(1, 4): " + subList);// [345, hello, insert3], 包括第一个索引位置,但是不包括最后索引位置数据//遍历数据for (Iterator iterator = arr.listIterator(); iterator.hasNext();) {System.out.println(iterator.next());}// java8提供流遍历arr.stream().forEach(element -> System.out.println(element));arr.stream().forEach(System.out::println);}}
0 0
- List接口案例
- List 接口
- List接口
- List 接口
- List接口
- List接口
- List接口
- List接口
- List接口
- List接口
- list接口
- List 接口
- List接口
- List接口
- List接口
- List接口
- List接口
- List接口
- 【HUSTOJ】1111: 单词调换位置
- TCP三次握手及四次挥手详细图解
- C++类型萃取
- Android之自定义标题--使用自定义Layout
- 菱形继承
- List接口案例
- Ubuntu16.04 Genymotion 模拟器上安装微信
- (4.1.23.10)自定义控件三部曲之动画篇(八)——PropertyValuesHolder与Keyframe
- Javascript基础知识盲点总结——数据类型和数组
- 放盘子
- tena 500g光猫获取超级密码和修改最大连接数
- Python greenlet
- Android广播机制
- how to answer Y in gdb script?