List的介绍

1. 什么是List

List是一个接口,继承自Collection。

Collection也是一个接口,该接口中规范了后序容器中常用的一些方法。

Iterable也是一个接口,表示实现该接口的类是可以逐个元素进行遍历。

2. 常见接口介绍

List中提供了好的方法,是常用方法如下:

|------------------------------------------|---------------------------|
| 方法 | 解释 |
| boolean add(E e) | 尾插 e |
| void add(int index, E element) | 将 e 插入到 index 位置 |
| boolean addAll(Collection c) | 尾插 c 中的元素 |
| E remove(int index) | 删除 index 位置元素 |
| boolean remove(Object o) | 删除遇到的第一个 o |
| E get(int index) | 获取下标 index 位置元素 |
| E set(int index, E element) | 将下标 index 位置元素设置为 element |
| void clear() | 清空 |
| boolean contains(Object o) | 判断 o 是否在线性表中 |
| int indexOf(Object o) | 返回第一个 o 所在下标 |
| int lastIndexOf(Object o) | 返回最后一个 o 的下标 |
| List subList(int fromIndex, int toIndex) | 截取部分 list |

java 复制代码
package Demo03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
//        boolean add(E e)	尾插 e
        strings.add("A");
        strings.add("B");
        strings.add("C");
        strings.add("D");
        strings.add("E");
        strings.add("F");
        System.out.println(strings);

//      void add(int index, E element)	将 e 插入到 index 位置
        strings.add(1, "H");
        System.out.println(strings);

//       boolean addAll(Collection c) 	尾插 c 中的元素
        List<String> strings1 = new ArrayList<>(Arrays.asList("A", "B", "C"));
        List<String> strings2 = new ArrayList<>(Arrays.asList("A", "B", "C"));
        strings1.addAll(strings2);
        System.out.println(strings1);

//        E remove(int index)	删除 index 位置元素
        strings.remove(2);
        System.out.println(strings);

//        boolean remove(Object o)	删除遇到的第一个 o
        strings.remove("A");
        System.out.println(strings);

//        E get(int index) 获取下标 index 位置元素
        System.out.println(strings.get(2));


//        E get(int index) 获取下标 index 位置元素
        strings.set(1, "G");
        System.out.println(strings);

//        void clear() 清空
        strings.clear();
        System.out.println(strings);

//        boolean contains(Object o) 判断 o 是否在线性表中
        List<String> strings3 = new ArrayList<>(Arrays.asList("ABC", "B", "C", "C", "D", "E"));
        System.out.println(strings3.contains("ABC"));
        System.out.println(strings3.contains("P"));

//        int indexOf(Object o) 返回第一个 o 所在下标
        System.out.println(strings3.indexOf("ABC"));


//        int lastIndexOf(Object o) 返回最后一个 o 的下标
        System.out.println(strings3.lastIndexOf("C"));

//        List<E> subList(int fromIndex, int toIndex) 截取部分 list
        System.out.println(strings3.subList(1, 3));
    }
}

3. List的使用

注意:List是个接口,并不能直接用来实例化。

如果要使用,必须去实例化List的实现类。在集合框架中,ArrayList和LinkedList都实现了List接口。

相关推荐
与己斗其乐无穷1 小时前
数据结构(2)线性表-顺序表
数据结构
周Echo周2 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list
小青龙emmm2 小时前
数据结构(一) 绪论
数据结构
矿渣渣2 小时前
AFFS2 的 `yaffs_ext_tags` 数据结构详解
数据结构·算法·文件系统·yaffs2
chenyuhao20243 小时前
链表的面试题4之合并有序链表
数据结构·链表·面试·c#
水水沝淼㵘4 小时前
嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
linux·服务器·c语言·数据结构·学习·算法·排序算法
莹莹学编程—成长记5 小时前
list基础用法
数据结构·list
清幽竹客5 小时前
redis数据结构-09 (ZADD、ZRANGE、ZRANK)
数据结构·数据库·redis
葵花日记6 小时前
数据结构——二叉树
c语言·数据结构
越城6 小时前
数据结构中的栈与队列:原理、实现与应用
c语言·数据结构·算法