淮安专业网站建设,网页设计代码水平对齐属性align,网站建设策划目的及过程,wordpress 一年好贵我们先看一下jdk1.9对其的描述#xff1a;什么是List#xff0c;也就是一个有序集合(序列)。1.List接口
List集合代表一个有序集合#xff0c;集合中每个元素都有其对应的顺序索引。List集合允许使用重复元素#xff0c;可以通过索引来访问指定位置的集合元素。
List接口继…我们先看一下jdk1.9对其的描述什么是List也就是一个有序集合(序列)。1.List接口
List集合代表一个有序集合集合中每个元素都有其对应的顺序索引。List集合允许使用重复元素可以通过索引来访问指定位置的集合元素。
List接口继承于Collection接口它可以定义一个允许重复的有序集合。因为List中的元素是有序的所以我们可以通过使用索引元素在List中的位置类似于数组下标来访问List中的元素这类似于Java的数组。
List接口为Collection直接接口。List所代表的是有序的Collection即它用某种特定的插入顺序来维护元素顺序。用户可以对列表中每个元素的插入位置进行精确地控制同时可以根据元素的整数索引在列表中的位置访问元素并搜索列表中的元素。实现List接口的集合主要有ArrayList、LinkedList、Vector、Stack。
1ArrayListArrayList是一个动态数组也是我们最常用的集合。它允许任何符合规则的元素插入甚至包括null。每一个ArrayList都有一个初始容量10该容量代表了数组的大小。随着容器中的元素不断增加容器的大小也会随着增加。在每次向容器中增加元素的同时都会进行容量检查当快溢出时就会进行扩容操作。所以如果我们明确所插入元素的多少最好指定一个初始容量值避免过多的进行扩容操作而浪费时间、效率。size、isEmpty、get、set、iterator 和 listIterator 操作都以固定时间运行。add 操作以分摊的固定时间运行也就是说添加 n 个元素需要 O(n) 时间由于要考虑到扩容所以这不只是添加元素会带来分摊固定时间开销那样简单。ArrayList擅长于随机访问。同时ArrayList是非同步的。
2LinkedList同样实现List接口的LinkedList与ArrayList不同ArrayList是一个动态数组而LinkedList是一个双向链表。所以它除了有ArrayList的基本操作方法外还额外提供了getremoveinsert方法在LinkedList的首部或尾部。由于实现的方式不同LinkedList不能随机访问它所有的操作都是要按照双重链表的需要执行。在列表中索引的操作将从开头或结尾遍历列表从靠近指定索引的一端。这样做的好处就是可以通过较低的代价在List中进行插入和删除操作。与ArrayList一样LinkedList也是非同步的。如果多个线程同时访问一个List则必须自己实现访问同步。一种解决方法是在创建List时构造一个同步的List List list Collections.synchronizedList(new LinkedList(...));
3Vector与ArrayList相似但是Vector是同步的。所以说Vector是线程安全的动态数组。它的操作与ArrayList几乎一样。
4StackStack继承自Vector实现一个后进先出的堆栈。Stack提供5个额外的方法使得Vector得以被当作堆栈使用。基本的push和pop 方法还有peek方法得到栈顶的元素empty方法测试堆栈是否为空search方法检测一个元素在堆栈中的位置。Stack刚创建后是空栈。以下来自JDK1.9 关于List
Public interface ListE extends CollectionEAn ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.一个有序集合也称为序列。这个接口的用户可以精确控制每个元素插入的列表的位置。用户可以通过他们的整数索引在列表中的位置访问元素并搜索列表中的元素。Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.与集合不同列表通常允许重复的元素。更正式的说列表通常允许成对的元素e1和e2比如e1.equalse2如果它们允许零元素它们通常允许多个空元素。有人可能希望实现一个禁止重复的列表在用户试图插入时抛出运行时异常这不是不可想象的但我们希望这种用法非常少见。The List interface places additional stipulations, beyond those specified in the Collection interface, on the contracts of the iterator, add, remove, equals, andhashCode methods. Declarations for other inherited methods are also included here for convenience.List接口将额外的规定放置在迭代器的契约、添加、移除、equals和hashcode方法上超出了集合接口中指定的条款。为了方便起见这里还包括了其他继承方法的声明。The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.List接口提供了四种方法来定位索引对列表元素的访问。列表如Java数组是基于0的。请注意这些操作可能会与某些实现的索引值成比例例如LinkedList类。因此如果调用者不知道实现那么遍历列表中的元素通常更可取。The List interface provides a special iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the Iterator interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.List接口提供了一个特殊的迭代器称为ListIterator除了Iterator接口提供的常规操作之外它还允许元素插入和替换以及双向访问。提供了一种方法来获得列表迭代器它从列表中指定的位置开始。The List interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.List接口提供了两种搜索指定对象的方法。从性能的角度来看这些方法应该谨慎使用。在许多实现中它们将执行代价高昂的线性搜索。The List interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.List接口提供了两种方法可以有效地在列表中的任意一点插入和删除多个元素。Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the equals and hashCode methods are no longer well defined on such a list.注意虽然列表可以将自己作为元素来包含但是要特别注意在这样的列表中equals和hashCode方法不再有很好的定义。Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as optional in the specification for this interface。一些列表实现对它们可能包含的元素有限制。
例如有些实现禁止零元素有些实现对其元素的类型有限制。
试图添加一个不合格的元素会抛出一个未经检查的异常通常是NullPointerException或ClassCastException。
试图查询不合格元素的存在可能会抛出异常或者它可能只是返回false;
一些实现将展示前者的行为而有些实现将展示后者。
更一般的情况是尝试对一个不符合条件的元素进行操作其完成不会导致将一个不合格的元素插入到列表中这可能会抛出异常或者在实现的选项中可能成功。
在该接口的规范中这些异常被标记为“可选”。