List接口基本介绍
- List集合类中元素有序,即添加顺序和取出顺序一致,且可重复;
- List集合中的每隔元素都有其对应的顺序索引,即支持索引
- List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素
- JDK API中List接口的实现类有:ArrayList LinkedList Vector
public class ListMethod {
public static void main(String[] args) {
List list = new ArrayList();
list.add("张三丰");
list.add("贾宝玉");
list.add(1,"林黛玉");
list.add("测试");
System.out.println(list);
//addAll
List list1 = new ArrayList();
list1.add("张三丰");
list1.add("贾宝玉");
list1.add(1,"林黛玉");
list.addAll(3,list1);
System.out.println(list);
System.out.println(list.get(5));
//indextOF
System.out.println(list.indexOf(list));
//lastIndexOf
System.out.println(list.lastIndexOf("贾宝玉"));
//remove
list.remove("贾宝玉");
//删除所有
list.removeAll(list1);
System.out.println(list);
//set 替换
list.set(0,"ceshi");
System.out.println(list);
//sublist 返回list对象前闭后开
List list2 = list.subList(0, 1);
System.out.println(list2);
}
}
标签:java,20230410,list,System,add,println,List,out
From: https://www.cnblogs.com/DragonJack/p/17299664.html