这是我学习时看到的一篇文章,觉得集合类讲的清晰、明了,就摘录下来!
在Java编程中,经常会用到Vector、Enumeration、ArrayList、Collection、Iterator、Set、List等集合类接口。
1. Vector类与Enumeration接口
Vector类是Java语言提供的一种高级数据结构,可用于保存一系列对象,Java不支持数组,Vector类提供了一种与"动态数组"相近的功能。如果我们不能预先确定要保存的对象的数目,或是需要方便获得某个对象的存放位置时,Vector类都是一种不错的选择。
下面的程序实现了在键盘上输入一个数字序列并存储在某种数据结构中,最后在屏幕上打印出每位数字相加的结果。
程序清单:TestVector.java
import java.util.*; //下面用到的Vector类和Enumeration接口都在此包中
/**
* Vector类与Enumeration接口
*/
public class TestVector
{
public static void main(String[] args)
{
int b = 0;
Vector v = new Vector();
System.out.println("Please Enter Number:");
while (true)
{
try
{
b = System.in.read();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
if (b== '/r' || b== '/n')
break;
else
{
int num = b- '0';
v.addElement(new Integer(num));
}
}
int sum = 0;
Enumeration e = v.elements();
while (e.hasMoreElements())
{
Integer intObj = (Integer)e.nextElement();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
运行结果:
输入32 打印5
输入1234 打印10
在上面的例子中,因为不能预先确定输入数字序列的位数,所以不能使用数组来存储每一个数值。正因为如此,我们选择了Vector类来保存数据。Vector.addElements只能接受对象类型的数据。先用Integer类包装了整数后,再用Vector.addElements方法向Vector对象中加入这个整数对象。最后,我们要取出保存在Vector对象中的所有整数进行相加,首先必须通过Vector.elements方法返回一个Enumeration接口对象,再用Enumeration.nextElement方法逐一取出保存的每个整数对象,Enumeration对象内部有一个提示器指向调用nextElement方法时要返回的对象的位置。
不要从字面上去理解nextElement方法,nextElement不是返回下一个对象,而是返回提示器正指向的那个对象,并将指示器指向下一个对象。当指示器指向了一个空对象(表示没有对象可以返回)时,Enumeration.hasMoreElement方法将返回false,否则返回true,调用nextElement方法之前,指示器指向第一个对象或空对象(Enumeration中没有一个对象存在时)。nextElement方法返回的是Object类型,需要对其进行类型转换,转换成Integer。
Enumeration是一个接口类,它提供了一种访问各种数据结构(Vector类只是众多数据结构中的一种)中的所有数据的抽象机制,就是我们要访问各种数据结构对象中的所有元素时,都可以使用同样的方式,调用同样的方法。有了这样的数据结构接口,就很容易学一百通,以不变应万变了。
2.Collection接口与Iterator接口
Collection接口的使用类似Vector类,只是方法的名称不同。我们要取出保存在实现Collection接口对象中的所有对象,我们也必须通过Collection.iterator方法返回一个Iterator接口对象,Iterator接口的功能与使用同Enumeration接口非常类似。Java2平台的数据结构类设计人员本可以扩展Enumeration接口,而不用创建Iterator这个新接口。但他们不喜欢Enumeration接口方法冗长的名字,因而创建了Iterator这个新接口,并缩短了方法名长度。
按照Java的语法,不能直接用Collection接口类创建对象,而必须用实现了Collection接口的类来创建对象,ArrayList类就是一个实现了Collection接口的类,我们将上面使用Vector和Enumeration的例子改为用ArrayList和Iteartor编写,就可以了解这些类之间的关系和用法 。
程序清单:TestCollection.java
import java.util.*; //ArrayList类和Iterator接口都在此包中
public class TestCollection
{
public static void main(String[] args)
{
int b = 0;
ArrayList al = new ArrayList();
System.out.println("Please Enter Number:");
while (true)
{
try
{
b = System.in.read();
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
if (b=='/r' | b== '/n')
break;
else
{
int num =b- '0';
al.add(new Integer(num));
}
}
int sum = 0;
Iterator itr = al.iterator();
while (itr.hasNext())
{
Integer intObj = (Integer)itr.next();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
运行结果与前面的TestVector.java相同:
输入32 打印5
输入1234 打印10
那么什么时候用Vector,什么时候用ArrayList呢?Vector类中的所有方法都是线程同步的,两个线程迸发访问Vector对象将是安全的,但只有一个线程访问Vector对象时,因为源程序仍调用了同步方法,需要额外的监视检查,运行效率要低些。
ArrayList类中的所有方法是异步的,所以在没有多线程安全问题时,最好用ArrayList,程序的效率会高些。在有线程安全问题,且我们的程序又没有自己处理(自己处理是指对调用ArrayList的代码或方法加上同步处理)时,只能用Vector。
2. 集合类接口的比较
另外还有几个集合类接口Set、List,下面是Collection和它们的比较。
Collection----对象之间没有指定的顺序,允许重复元素。
Set----对象之间没有指定的顺序,不允许重复元素。
List----对象之间有指定的顺序,允许重复元素。
这三个接口的继承关系如图所示:
多学两招:
由于在List接口中,对象之间有指定的顺序。因此我们可以对List接口的对象进行排序。
程序清单:TestSort.java
import java.util.*;
public class TestSort
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add(new Integer(1));
al.add(new Integer(3));
al.add(new Integer(2));
System.out.println(al.toString());//排序前
Collections.sort(al);
System.out.println(al.toString());//排序后
}
}
运行结果:
[1, 3, 2]
[1, 2, 3]
在上面的程序中,我们向实现了List接口的ArrayList类对象中添加了3个成员。然后用Collections类的sort静态方法对其进行排序。(完)