Java:List(二)

2021/6/18 22:59:52

本文主要是介绍Java:List(二),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

模块:java.util.List

说明

List接口,表明一个序列

用法:List <E>

①直接用List承接一个ArrayList或LinkedList

List <Integer> list = new ArrayList <> ();

②用List.of( ... )快速创建List

List <String> list = List.of("1","2","3");

方法

返回值类型

方法

说明

boolean add(E e) 向List末尾加入一个元素e(相当于Python中的List.append(e))
void add(int index , E e) 在index处插入e
boolean addAll( Collection<? extends E> c )

向List末尾加入c中的所有元素

(相当于Python中的List.extend(c))

boolean addAll(int index , Collection c) 在index处插入c
void clear() 移除List中的所有元素
boolean contains(Object o) 判断List中是否包含o
boolean contains(Collection <?> c) 判断List中是否包含c中全部元素
boolean equals(Object o) 判断两个List是否相等——大小、元素全部相等
E get(int index) 获取指定索引处的元素
int hashCode() 获取该List的hashCode
int indexOf(Object o) 获取o在该List中的索引,如果不存在则返回-1
boolean isEmpty() List为空返回true
Iterator<E> iterator() 返回一个Iterator,可以用它遍历全部元素
int lastIndexOf(Object o) 获取o最后一次出现时的索引,不存在则返回-1
ListIterator<E> listIterator() 返回一个listIterator
ListIterator<E> listIterator(int index) 返回一个从指定index处开始迭代的listIterator
E remove(int index) 移除并返回指定索引处的元素
boolean remove(Object o) 移除List中第一次出现的元素o
boolean removeAll(Collection<?> c) 移除List中所有在集合c中的元素
default void replaceAll(UnaryOperator <E> operator) 对所有List中的元素应用某个运算符,并用运算结果替换List中的元素
boolean retainAll(Collection<?> c) 只保留List中那些存在于集合c中的元素
E set(int index , E element) 替换指定索引处的元素
int size() 返回List的大小
default void sort(Comparator<? super E> c) 用指定Comparator对List中的元素进行排序
default Spliterator<E> splitertor() 对List中的元素创造一个Spliterator
List<E> subList(int start , int end) 返回[start,end)间的子List
Object[] toArray() 返回一个array,其中包含了List中的所有元素
<T> T[] toArray(T[] a) 把List中的元素保存到一个指定类型T的array中


这篇关于Java:List(二)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程