提交 28fd80d3 编写于 作者: A Allenyep

2th

上级 f1d5b1e1
...@@ -102,4 +102,68 @@ int sum (int[] A) { ...@@ -102,4 +102,68 @@ int sum (int[] A) {
### 2.1.2 ListIterator接口 ### 2.1.2 ListIterator接口
有些数据集合的确有排序的自然概念,但从按索引的集合中提取任意项的代价可能仍然很昂贵。例如,您可能看到了Scheme语言中的链表:给定列表中的一个元素,它需要n个操作来确定第n个后续元素(与Java数组相反,Java数组只需要一个步骤或一些特定操就可以检索任何项。)。标准Java库包含接口**java.util.ListIterator**,它通过有序序列进行排序的,而不通过索引来获取每个序列。如代码2.2所示。除了迭代器的“查询”方法和**remove**方法之外,**Listaterator**类提供了在集合中插入新项或替换项的操作。 有些数据集合的确有排序的自然概念,但从按索引的集合中提取任意项的代价可能仍然很昂贵。例如,您可能看到了Scheme语言中的链表:给定列表中的一个元素,它需要n个操作来确定第n个后续元素(与Java数组相反,Java数组只需要一个步骤或一些特定操就可以检索任何项。)。标准Java库包含接口**java.util.ListIterator**,它通过有序序列进行排序的,而不通过索引来获取每个序列。如代码2.2所示。除了迭代器的“查询”方法和**remove**方法之外,**Listaterator**类提供了在集合中插入新项或替换项的操作。
## 2.2 Java集合抽象 ## 2.2 Java抽象集合框架
\ No newline at end of file
Java库(从JDK 1.2开始)提供代表各种集合的接口层次结构,加上抽象类的层次结构,以帮助程序员提供这些接口的实现,以及一些实际(“具体”)的实现。这些类都在包**java.util**中找到。图2.4说明了专门用于集合的类和接口的层次结构。
### 2.2.1 集合框架接口
Java库接口**java.util.Collection**其方法总结在图2.5和2.6中,描述了包含值集合的数据结构,其中每个值都是对某个Object(或null)的引用。与“集合(Collection)”相对的术语“集(set)”也出现在这里,因为Collection可以像数学上定义的集合那样描述多集合(bags)。
``` java
package java.util;
/** Abstraction of a position in an ordered collection. At any* given time, THIS represents a position (called its cursor)* that is just after some number of items of type T (0 or more) of* a particular collection, called the underlying collection. */
public interface ListIterator<T> extends Iterator<T> {
/* Exceptions: Methods that return items from the collection throw* NoSuchElementException if there is no appropriate item. Optional* methods throw UnsupportedOperationException if the method is not* supported. */
/* Required metho ds: */
/** True unless THIS is past the last item of the collection */
boolean hasNext ();
/** True unless THIS is before the first item of the collection */
boolean hasPrevious ();
/** Returns the item immediately after the cursor, and* moves the current position to just after that item.* Throws NoSuchElementException if there is no such item. */
T next ();
/** Returns the item immediately before the cursor, and* moves the current position to just before that item.* Throws NoSuchElementException if there is no such item. */
T previous ();
/** The number of items before the cursor */
int nextIndex ();
/* nextIndex () - 1 */
int previousIndex ();
/* Optional methods: *//** Insert item X into the underlying collection immediately before* the cursor (X will be returned by previous()). */
void add (T x);
/** Remove the item returned by the most recent call to .next ()* or .previous (). There must not have been a more recent* call to .add(). */
void remove ();
/** Replace the item returned by the most recent call to .next ()* or .previous () with X in the underlying collection.* There must not have been a more recent call to .add() or .remove. */
void set (T x);
}
```
代码2.2 java.util.ListIterator接口
【图2.3】
图2.3 Java库的Map相关类型(来自java.util)。椭圆表示接口;虚线框是抽象类,实心框是具体(非抽象)类。实线箭头表示继承(extends)关系,虚线箭头表示实现(implements)关系。抽象类供希望添加新集合类的实现用。它们提供了某些方法的默认实现。程序员将new应用于具体类以获取实例,并且(理想情况下)使用接口作为形式参数类型,以便尽可能广泛地使用方法。
【图2.4】
图2.4 Java库的Collection类型(来自java.util).有关符号,请参见图2.3
由于这是一个接口,描述操作的文档注释可能不准确。一个无能或恶作剧的程序员可以编写一个实现Collection的类,将add方法改成remove方法。尽管如此,正经的程序员都会根据注释接受Collection的方法编写程序。C作为一个正常的集合,在执行C.add(x)之后,x将被放进C中。
并非每种集合都需要实现每种方法。具体而言,图2.6中不是可选方法,但可能会抛出选择提升标准异常UnsupportedOperationException。有关此特定设计选择的进一步讨论参见2.5节。仅实现所需方法的类本质上是只读集合;它们一旦被创造就无法修改。
关于图2.5中的构造函数的注释仅仅是一个注释。Java接口没有构造函数,因为它们不代表具体对象的特定类型。然而你首先需要一些构造函数来创建一个Collection,而注释的目的是告诉你一些有用的统一标准。
在这一点上,你可能想知道Collection类可能是什么样的,因为不可能直接创建一个它的实例(它是一个接口),并且缺少有关其成员的功能的详细信息(例如,给定的Collection可以有两个相同的元素吗?)。重点是使用Collection接口中提供的信息编写的任何函数都适用于Collection的所有实现类。
例如,这里有一个简单的方法来确定一个Collection的元素是否是另一个的子集:
``` java
/** True iff C0 is a subset of C1, ignoring repetitions. */
public static boolean subsetOf (Collection<?> C0, Collection<?> C1) {
for (Object i : C0)
if (! C1.contains (i))
return false;
// Note: equivalent to
// for (Iterator<?> iter = C0.iterator(); iter.hasNext (); ) {
// Object i = iter.next ();
// ...
return true;
}
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册