JAVA编程思想(第四版)学习笔记----11.5 List,11.6迭代器

Collection类的层次结构图(来源与网络)如下所示:

接口:Iterator<T>
public interface Iterable<T>
Iterable<T>接口作为超级接口,此接口中只有一个返回类型为Iterable<t> 的iterator()方法,实现这个接口允许对象成为 "foreach" 语句的目标。


接口:Collection<T>
public interface Collection<E> extends Iterable<E>

Collection层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素(如List,Queue),而另一些则不允许(如Set)。一些 collection 是有序的(如List,TreeSet,LinkedHashSet,TreeMap,LinkedHashMap),而另一些则是无序的(如HashSet,HashMap)。JDK 不提供此接口的任何直接实现:它提供更具体的子接口(如 SetList)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。

接口:List<T>

public interface List<E> extends Collection<E>

有序的 collection(也称为序列)。List接口在Collection接口的基础上添加了大量的方法,使得可以对列表中每个元素的插入或移除位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。

List接口在 iteratoraddremoveequalshashCode 方法的协定上加了一些其他约定,超过了 Collection 接口中指定的约定。

List 接口提供了对列表元素进行定位(索引)访问方法。列表(像 Java 数组一样)是基于 0 的。注意,这些操作可能在和某些实现(例如 LinkedList 类)的索引值成比例的时间内执行。因此,如果不知道具体实现,那么在列表元素上迭代(Iterator,或者foreach循环)通常优于用索引遍历(for循环)列表。

List 接口提供了特殊的迭代器,称为 ListIterator,除了允许 Iterator 接口提供的正常操作外,该迭代器还允许元素插入和替换,以及双向访问。还提供了一个方法来获取从列表中指定位置开始的列表迭代器。

抽象类:AbastractCollection<T>

public abstract class AbstractCollection<E> extends Object implements Collection<E>

此类提供 Collection 接口的骨干实现,实现了Collection接口的List、Set、Queue接口的实现类都可以继承此抽象类,以最大限度地减少了实现此接口所需的工作。

该接口实现了除size,iterator之外的其他Collection中的接口,并对add方法进行了限制。

要实现一个不可修改的 collection,编程人员只需扩展此类,并提供 iteratorsize 方法的实现。(iterator 方法返回的迭代器必须实现 hasNextnext。)

要实现可修改的 collection,编程人员在扩展此类并提供iterator和size方法的实现外,还必须另外重写此类的 add 方法(否则,会抛出 UnsupportedOperationException),iterator 方法返回的迭代器还必须另外实现其 remove 方法。

抽象类:AbastractList<T>

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

此类提供 List 接口的骨干实现,以最大限度地减少实现“随机访问”数据存储(如数组ArrayList)支持的该接口所需的工作(也就是实现了List接口扩展自Collection接口外的方法)。对于实现Collection接口中的方法,此类则是通过继承AbstractCollection抽象类来实现。此类没有实现的方法包括get(int index)和size(),并对set(int index, E element),add(int index,E element),remove(int index),add(E element)方法进行了限制。

对于连续的访问数据(如链表LinkedList),应优先使用 AbstractSequentialList,而不是此类。

要实现不可修改的列表,编程人员只需扩展此类,并提供 get(int)size() 方法的实现。

要实现可修改的列表,编程人员必须另外重写 set(int, E) 方法(否则将抛出 UnsupportedOperationException)。如果列表为可变大小,则编程人员必须另外重写 add(int, E)remove(int) 方法。

类:ArrayList<T>

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

ArrayList是List接口的大小可变的基于数组的实现。其底层采用数组的实现方式,所以具有良好的随机访问能力,但是对于在指定位置进行插入、删除等操作的效率不高。

每个 ArrayList 实例都有一个容量。该容量是指用来存储列表元素的数组的大小,没有指定ArrayList容量大小时,创建的实例默认容量为10。随着向 ArrayList 中不断添加元素,其容量也自动增长。

此类的 iteratorlistIterator 方法返回的迭代器是快速失败的:在创建迭代器之后,除非通过迭代器自身的 removeadd 方法从结构上对列表进行修改,否则在任何时间以任何方式对列表进行修改,迭代器都会抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就会完全失败,而不是冒着在将来某个不确定时间发生任意不确定行为的风险。

时间: 2024-08-07 16:48:47

JAVA编程思想(第四版)学习笔记----11.5 List,11.6迭代器的相关文章

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十三)之Strings

Immutable Strings Objects of the String class are immutable. If you examine the JDK documentation for the String class, you’ll see that every method in the class that appears to modify a String actually creates and returns a brand new String object c

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记四)之Operators

At the lowest level, data in Java is manipulated using operators Using Java Operators An operator takes one or more argument and produces a new value. The arguements are in a different form than ordinary method calls, but the effect is the same. + :

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(二)之Introduction to Objects

The genesis of the computer revolution was a machine. The genesis of out programming languages thus tends to look like that machine. 计算机革命起源于机器,因此编程语言的产生也始于对机器的模仿 Computers are mind amplification tools and a different kind of expressive medium. 计算机是大

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects

To solve the general programming problem, you need to create any number of objects, anytime, anywhere. So you can't rely on creating a named reference to hold each one of your objects. Java has several ways to hold objects: 1. the compiler-supported

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(六)之Initialization &amp; Cleanup

Two of these safety issues are initialization and cleanup. initialization -> bug cleanup -> running out of resources (most notably, memory) Java adopted the constructor, and in addition has a garbage collector that automoatically releases memory res

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism

Polymorphism is the third essential feature of an object-oriented programming language,after data abastraction and inheritance. It provides another dimension of separation of interface from implementation, to decouple what from how. Polymorphism allo

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Reusing Classes

The trick is to use the classes without soiling the existing code. 1. composition--simply create objects of your existing class inside the new class. simply reusing the functionality of the code, not its form 2.inheritance--creates a new class as a t

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information

Runtime type information (RTTI) allow you to discover and use type information while a program is running This take two forms: 1. "traditional" RTTI, which assumes that you have all the types available at compile time, 2. the reflection mechanis

Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

The ideal time to catch an error is at compile time, before you even try to run the program. However, not all errors can be detected at compile time. To create a robust system, each component must be robust. By providing a consistent error-reporting

Java编程思想第四版读书笔记——第十三章 字符串

Java编程思想第四版读书笔记--第十三章 字符串 字符串的操作是计算机程序设计中最常见的行为. 关键词: StringBuilder ,StringBuffer,toString(),format转换,正则表达式, 1.不可变String String对象时不可变的.每当把String对象作为方法的参数时,都会复制一份引用.(其实就是对函数中参数列表中参数的操作不会影响外面的原参数) 如下: import static net.mindview.util.Print.*; public cla