List接口
接口描述
1、 实现List接口的集合类中的元素是有序的,且允许重复。
2、 List集合中的元素都对应一个整数型的序号记载其在集合中的位置,可以根据序号存取集合中的元素。
JDK API所提供的List集合类常用的有:
ArrayList
LinkedList
使用方式
List常用方法
代码演示
集合的增加
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add("zhangsan");
list.add("lisi");
list.add("wangwu");
集合的删除
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.remove(0);
list.remove(1);
list.remove(2);
集合的修改
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.set(0,"wangwu");
list.set(1,"zhangsan");
list.set(2,"lisi");
集合查看指定位置的元素
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.get(0);
list.get(1);
list.get(2);
//在获取元素后需要使用System.sout.println()一起使用;
总结
集合相对于数组来说是一种升级,数组适用的场景一般是固定的数量,而集合适用的场景是要经常增删改的数据。