当前位置: 首页 > news >正文

学习笔记21 list

一、概述

有两种不同的方法来实现List接口。ArrayList类使用基于连续内存分配的实现,而LinkedList实现基于linked allocation

list接口提供了一些方法:

 二、The ArrayList and LinkedList Classes

1.构造方法

这两个类有相似的构造方法:

ArrayList()ArrayList(int initialCapacity)ArrayList(Collection<? extends E> c)
LinkedList()LinkedList(Collection<? extends E> c)

使用第三种构造方法可以将list转为arraylist:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;public class ArrayListDemo {public static void main(String[] args) {// 创建一个包含一些整数的列表List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);// 使用 ArrayList(Collection<? extends E> c) 构造方法创建一个新的 ArrayList 对象ArrayList<Integer> arrayList = new ArrayList<>(numbers);// 打印 ArrayList 的内容System.out.println(arrayList);}
}

用for循环将数组转为linkedlist: 

// Initialize array of names.String [] namesArray = {"Anna", "Bob", "Carlos", "Debby"};// Create empty list.LinkedList<String> list1 = new LinkedList<>();// Populate the list using a loop.for (String s : namesArray){list1.add(s);}System.out.println(list1);

注意,java中的collection都自带toString方法,结果如下:

[Anna, Bob, Carlos, Debby]

 2.asList

我们可以不用for循环遍历,而直接把一个array转化为linkedlist或arraylist:

// Initialize array of names.
String[] namesArray = {"Anna", "Bob", "Carlos", "Debby"};// Create LinkedList from array using constructor.LinkedList<String> linkedList = new LinkedList<>(Arrays.asList(namesArray));

也可以这样:

LinkedList list2 =new LinkedList<>(Arrays.asList("Anna", "Bob", "Carlos", "Debby"));

3.linkedlist中的其他方法 

三、List Iterators

迭代器可以在列表中向前或向后移动。除了 next() 方法外,列表迭代器还有一个 previous() 方法,列表迭代器有一个光标位置,它总是位于调用 previous() 方法返回的元素和调用 next() 方法返回的元素之间。

最初,光标位于索引为 0 的元素之前。调用 next() 时,光标将返回索引为 0 的元素,并越过返回的元素向前移动到元素 0 和元素 1 之间的位置。此时,调用 previous() 将返回元素 0,并将光标移回起点,但调用 next() 将返回元素 1,并将光标向前移动到元素 1 和元素 2 之间的位置。如果光标已经越过最后一个元素,调用 next() 将抛出 NoSuchElementException 异常。同样,如果光标位置在元素 0 之前,调用 previous() 也会产生同样的异常。

列表迭代器中的 remove() 方法会从列表中删除最后一次调用 next() 或 previous() 时返回的元素

也就是说,如果在调用 `remove()` 方法之前没有先调用 `next()` 或 `previous()` 方法,那么会抛出一个 `IllegalStateException` 异常。

这是因为 `remove()` 方法的作用是删除迭代器最后访问的元素,所以在调用 `remove()` 方法之前必须先调用 `next()` 或 `previous()` 方法,以确保迭代器已经指向了要删除的元素。

如果在调用 `remove()` 方法之前没有先调用 `next()` 或 `previous()` 方法,那么迭代器没有指向任何元素,也就无法删除元素,因此会抛出 `IllegalStateException` 异常。

这里的迭代器的remove方法和list的remove方法不是一个东西,自然不能按原来的方式理解。

list的迭代器还提供了以下方法:

 例子:

import java.util.*;/*** Demonstrates the list iterator*/
public class ListIteratorDemo {public static void main(String[] args) {// Create an array list to hold strings.String[] names = {"Chris", "David", "Katherine", "Kenny"};List<String> nameList = new ArrayList<>(Arrays.asList(names));// Display the names in the list.System.out.println("Here are the original names.");System.out.println(nameList);// Get a list iterator.ListIterator<String> it = nameList.listIterator();// Add "Darlene" to the list right after "Chris".while (it.hasNext()) {String str = it.next();// If the last name retrieved was "Chris"// then insert "Darlene".if (str.equalsIgnoreCase("Chris")) {it.add("Darlene");// We are donebreak;}}//Display the names in the list again.System.out.println("\nHere are the new names now.");System.out.println(nameList);}
}

Here are the original names.

[Chris, David, Katherine, Kenny]

Here are the new names now.

[Chris, Darlene, David, Katherine, Kenny]

 四、用list接口变量去引用linkedlist或arraylist

推荐使用

List<String> nameList = new ArrayList<>();

而不是

 ArrayList<String> nameList = new ArrayList<>();

因为第一种方法后期容易修改。

比如在后期发现namelist实际上应该使用linkedlist,这是只需要把第一句改成:

List<String> myList = new LinkedList<>();

这时,项目中所有的myList就都变成了Linkedlist。

如果用第二种方法,我们在项目中别的地方传参的时候,会传入(arraylist nameList),这时如果要换成Linkedlist,我们需要改变所有传入参数的类型。

使用第一种方法,我们传的参数是(List nameList),就不需改变。

http://www.lryc.cn/news/100128.html

相关文章:

  • 微信小程序弱网监控
  • 【Linux】进程通信 — 共享内存
  • “从零开始学习Spring Boot:快速搭建Java后端开发环境“
  • 行为型-状态模式(State Pattern)
  • 大厂领导为什么喜欢跨层与下属聊天
  • Android 面试题 避免OOM(内存优化)三
  • SpringBoot集成Lock4j 底层使用Redission 实现分布锁
  • TortoiseSVN操作使用
  • 第五篇-ChatGLM2-6B模型下载
  • 【Matlab】基于长短期记忆网络的数据分类预测(Excel可直接替换数据)
  • C++网络编程 TCP套接字基础知识,利用TCP套接字实现客户端-服务端通信
  • 苍穹外卖-day07
  • 简化Java单元测试数据
  • P1041 [NOIP2003 提高组] 传染病控制
  • TypeScript -- 基础类型
  • Cookie 与 Session 的作用及区别、结合使用
  • 【Redis】面试题
  • (学习笔记-硬件结构)CPU如何执行程序?
  • curl: (26) Failed to open/read local data from file/application
  • 2023年深圳杯数学建模 D题 基于机理的致伤工具推断
  • DMA传输原理与实现详解(超详细)
  • 【《React Hooks实战》——指导你使用hook开发性能优秀可复用性高的React组件】
  • Ajax详细讲解
  • 黑苹果如何在macOS Sonoma中驱动博通网卡
  • JVM-Cpu飙升排查及解决
  • exoplayer3 ffmpeg 扩展库编译 aar,导入集成
  • Shell免交互
  • 设计模式之四:工厂模式
  • 斩获CVPR 2023竞赛2项冠军|美团街景理解中视觉分割技术的探索与应用
  • UE4/5C++多线程插件制作(十五、将模板统一,修改统一后的其他类,修改继承,修改返回类型等)