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 不提供此接口的任何直接实现:它提供更具体的子接口(如 Set 和 List)实现。此接口通常用来传递 collection,并在需要最大普遍性的地方操作这些 collection。
接口:List<T>
public interface List<E> extends Collection<E>
有序的 collection(也称为序列)。List接口在Collection接口的基础上添加了大量的方法,使得可以对列表中每个元素的插入或移除位置进行精确地控制。用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素。
List接口在 iterator、add、remove、equals 和 hashCode 方法的协定上加了一些其他约定,超过了 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,编程人员只需扩展此类,并提供 iterator 和 size 方法的实现。(iterator 方法返回的迭代器必须实现 hasNext 和 next。)
要实现可修改的 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 中不断添加元素,其容量也自动增长。
此类的 iterator 和 listIterator 方法返回的迭代器是快速失败的:在创建迭代器之后,除非通过迭代器自身的 remove 或 add 方法从结构上对列表进行修改,否则在任何时间以任何方式对列表进行修改,迭代器都会抛出 ConcurrentModificationException。因此,面对并发的修改,迭代器很快就会完全失败,而不是冒着在将来某个不确定时间发生任意不确定行为的风险。
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 fo ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(四)之Operators
At the lowest level, data in Java is manipulated using operators Using Java Operators An operator ta ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(三)之Everything Is an Object
---恢复内容开始--- Both C++ and Java are hybird languages. A hybird language allow multiple programming st ...
- 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 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十四)之Type Information
Runtime type information (RTTI) allow you to discover and use type information while a program is ru ...
- 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 ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十一)之Holding Your Objects
To solve the general programming problem, you need to create any number of objects, anytime, anywher ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes
The inner class is a valuable feature because it allows you to group classes that logically belong t ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(九)之Interfaces
Interfaces and abstract classes provide more structured way to separate interface from implementatio ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(八)之Polymorphism
Polymorphism is the third essential feature of an object-oriented programming language,after data ab ...
随机推荐
- windows10简单试用(多图,连薛定谔的猫都杀死了)
为了大家看起来方便,我的截图都是gif的,比较小,但是颜色会有色差,相信大家不介意的 昨天windows10可以下载第一时间就下了玩玩 由于是技术预览,所以不打算替换之前的系统,只装在虚拟机里玩玩就好 ...
- datepickerx设置默认日期
datepicher插件是jQuery UI的一个插件,它提供一个日期弹出窗口(或直接显示在页面),供用户选择日期.在Web开发中,总会遇到需要用户输入日期的情况.一般都是提供一个text类型的inp ...
- C#设计模式-状态者模式
一. 状态者(State)模式 每个对象都有其对应的状态,而每个状态又对应一些相应的行为,如果某个对象有多个状态时,那么就会对应很多的行为.那么对这些状态的判断和根据状态完成的行为,就会导致多重条件语 ...
- idea怎么设置自己的名字和时间
1.直接修改idea64.exe.vmoptions 里面添加上 -Duser.name=yourname 重启即可生效 1.file - settings-Editor- File and Code ...
- Chrome调试手机页面
新开发的网页需要在手机或是模拟机上运行测试,如果手头事件比加紧,那么可以借助 Chrome提供的手机网页预览程序进行简单调试.查看 制作的网页是否能够适合各种手机型号使用. 下面所以下如何使用Chro ...
- 前端开发面试题收集(js部分)
1.问:js中"1"+2+"3"+4 运算结果是? 答: js中,字符串和数值相加,得到的还是字符串,这里的结果1234也是字符串. 2.问:4+3+2+&qu ...
- Android数据加密之Aes加密
前言: 项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes ...
- git常用操作命令
使用git进行版本控制,分为两部分: 一: 服务端 1.1 首先要申请一个git的账号,方便团队协作.推荐开源中国(www.oschina.net),相对于github来说,有两个优点:1.访问速度很 ...
- SharePoint2013 Set a custom application page as site welcome page
本文主要介绍如何添加一个custom application page as site welcome page 1.首先创建一个sharepoint 2013 empty solution, add ...
- Linux平台 Oracle 11gR2 RAC安装Part2:GI安装
三.GI(Grid Infrastructure)安装 3.1 解压GI的安装包 3.2 安装配置Xmanager软件 3.3 共享存储LUN的赋权 3.4 使用Xmanager图形化界面安装GI 3 ...