List接口

概述

List接口继承自Collection接口,是单列集合的一个重要分支,习惯性的将实现了List接口的对象称之为List集合[1]。List集合允许出现重复元素,所有元素以一种线性方式存储,程序中可以通过索引访问集合中的指定元素。

List接口常用方法

List作为Collection子接口,不但继承了Collection中所有方法[2],还增加了一些根据元素索引操作集合的特有方法。

方法 描述
void add(int index,Object element) 将元素插入在List集合的index处
boolean addAll(int index,Collection c) 将集合c包含的所有元素插入到List集合的index处
Object get(int index) 返回集合索引index处的元素
Object remove(int index) 删除index索引处的元素
Object set(int index,Object element) 将索引index处的元素替换成element对象,并将替换后的element对象返回
int indextOf(Object o) 返回对象o在List集合中第一次的位置索引
int lastIndexOf(Object o) 返回对象o在List集合中最后一次的位置索引
List subList(int fromIndex,int toIndex) 返回从索引fromIndex开始到toIndex前一个索引处所有元素组成的子集合

  1. List集合详见List实现类 ↩︎

  2. Collection中所有方法详见Collection接口 ↩︎

posted on 2021-10-15 00:38  技术小伙伴  阅读(139)  评论(0)    收藏  举报