Skip to Content
Java 泛型与集合,第二版
book

Java 泛型与集合,第二版

by Maurice Naftalin, Philip Wadler
June 2025
Intermediate to advanced
368 pages
4h 53m
Chinese
O'Reilly Media, Inc.
Content preview from Java 泛型与集合,第二版

第 14 章 列表 清单

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在实践中,列表可能是使用最广泛的 Java 集合。列表是一种集合,它(与集合不同)可以包含重复的元素,并且(与队列不同)允许用户完全查看和控制其元素的排序。相应的集合框架接口是List<E> 。

List 合约重载了equals 方法;它规定List 只能等于另一个List ,而且只有当它们包含相同元素且顺序相同时才可以。hashCode 方法也被重载,当equals 被重载时,情况也应如此(参见"实现"部分中的 "散列表")。List 接口文档规定了根据List 元素的散列代码计算其hashCode 所使用的算法。

列表界面方法

List 接口公开了方法,用于定位访问、搜索给定值、生成视图以及创建ListIterators--这是Iterator 的子类型,具有利用List的顺序特性的附加功能。此外,从SequencedCollection 继承来的方法提供了常用位置访问调用的便捷简短版本。最后,静态工厂方法可用于创建不同长度的不可修改列表。

定位访问方法

void add(int index, E e)
e 在给定的index
boolean addAll(int index, Collection<? extends E> c)
将c 的内容添加到给定的index
E get(int index)
返回给定index
E remove(int index)
删除指定index
E set(int index, E e)
将位于index 的元素替换为e

这些方法根据在列表中的数字位置访问元素。在List<Integer> 的情况下,remove 的声明与继承方法Collection::remove 有一个不幸的冲突。尽管重载解析规则明确规定,像list.remove(0) 这样的调用指的是索引为 0 的元素,但这是程序员经常感到困惑的地方,因此也是经常出错的地方。

搜索方法

int indexOf(Object o)
返回o
int lastIndexOf(Object o)
返回o

这些方法在列表中搜索指定的对象,并返回其数字位置,如果对象不存在,则返回-1 。

视图生成方法

List<E> subList(int fromIndex, int toIndex)
返回部分列表的视图
List<E> reversed()
提供原始藏品的逆序视图

这些方法提供了List 的不同视图(见"视图")。方法subList 的工作方式与subSet 对SortedSet 的操作类似(见"NavigableSet"),但它使用的是元素在列表中的位置而不是其值:返回的列表包含从fromIndex 开始的列表元素,直到但不包括toIndex 。返回的列表并不单独存在;它只是从中获取的部分列表的视图,其中的所有变化都会反映在原始列表中。但这里与subSet 有一个重要区别:对子列表的更改会写入后备列表,但只有非结构性更改才会反过来。如果不通过子列表,而是通过插入或删除的方式对后备列表进行任何结构性更改,那么以后再尝试使用子列表时,将导致 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java Server Pages

Java Server Pages

Hans Bergsten
JUnit in Action

JUnit in Action

Ted Husted, Vincent Massol

Publisher Resources

ISBN: 9798341661035