阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

List集合

110次阅读
没有评论

共计 2588 个字符,预计需要花费 7 分钟才能阅读完成。

1、List 概述

1.1、什么是 List

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

List 集合中元素有序,即元素的存入顺序和取出顺序一致。

1.2、List 特点

List 是一个元素存取有序的集合。例如,存元素的顺序是 11、22、33。那么集合中,元素的存储就是按照 11、22、33 的顺序完成的

List 是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)

List 中可以有重复的元素,通过元素的 equals 方法,来比较是否为重复的元素

1.3、List 子类

ArrayList:底层数据结构是数组结构。线程不安全的。所以 ArrayList 的出现替代了 Vector。增删慢, 查找快。

LinkedList:底层是链表数据结构。线程不安全的,同时对元素的增删快, 查找慢。

Vector:底层数据结构是数组结构。jdk1.0 版本。线程安全的。无论增删还是查询都非常慢,已被 ArrayList 替代。

2、List 常用方法

void add(int index, E element) // 指定索引添加元素

E remove(int index) // 移除指定索引处元素

E get(int index) // 获取指定索引元素

E set(int index, E element) // 修改指定索引元素

List <E> subList(int fromIndex, int toIndex) // 截取指定索引子集

int indexOf(Object o) // 返回指定元素索引位置

import java.util.ArrayList; import java.util.List; public class ListDemo01 {public static void main(String[] args) {fun3(); fun2(); fun();} private static void fun3() {List<String> al = new ArrayList<>(); al.add("111");// 添加 Collection 中 继承来的 al.add("222"); al.add("333"); al.add("444"); System.out.println(al); // 修改指定索引元素 al.set(2, "6666"); System.out.println(al); // 修改指定索引元素 List<String> subList = al.subList(1, 4); System.out.println(subList); // 返回指定元素索引位置 int indexOf = subList.indexOf("444"); System.out.println(indexOf); } private static void fun2() {List<Integer> al = new ArrayList<>(); al.add(1); al.add(2); al.add(3); al.add(4); // 移除指定索引处元素 al.remove(2); System.out.println(al); // 获取指定索引元素 System.out.println(al.get(2)); } private static void fun() {List<String> al = new ArrayList<>(); al.add("111");// 添加 Collection 中 继承来的 al.add("222"); al.add("333"); al.add("444"); // 指定索引添加元素 al.add(2, "666"); al.add(5, "999"); al.add(6, "000999"); System.out.println(al); // 移除指定索引处元素 al.remove(6); System.out.println(al); } }

ListIterator <E> listIterator() 注意:用于应对并发修改异常的返回迭代器方法与迭代器

import java.util.*; public class ListDemo02 {public static void main(String[] args) {fun(); fun1();} private static void fun() {Collection c= new ArrayList(); c.add("孙悟空"); c.add("白骨精"); c.add("唐三藏"); c.add("八戒"); // 遍历 集合, 如果 包含 白骨精 , 你就添加一个 白龙马. Iterator iterator = c.iterator(); while(iterator.hasNext()) {Object object = (Object) iterator.next(); if (object.equals("白骨精")) {// c.add("白龙马"); //ConcurrentModificationException } } System.out.println(c); } private static void fun1(){List c = new ArrayList(); c.add("孙悟空"); c.add("白骨精"); c.add("唐三藏"); c.add("八戒"); ListIterator li= c.listIterator(); while (li.hasNext()) {Object object = (Object) li.next(); if (object.equals("白骨精")) {li.add("白龙马"); } } System.out.println(c); while (li.hasPrevious()) {System.out.print(li.previous() + " "); } System.out.println(); ListIterator listIterator = c.listIterator(); System.out.println(listIterator.next()); // 与 hasNext() 相反,往前遍历 System.out.println(listIterator.hasPrevious()); } }

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-06-06发表,共计2588字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中