Java工具包提供了强大的数据结构。在Java中的数据结构主要包括以下几种接口和类:
- 枚举(Enumeration)
- 位集合(BitSet)
- 向量(Vector)
- 栈(Stack)
- 字典(Dictionary)
- 哈希表(Hashtable)
- 属性(Properties)
以上这些类是传统遗留的,在Java2中引入了一种新的框架-集合框架(Collection)。
枚举(Enumeration)
枚举(Enumeration)接口虽然它本身不属于数据结构,但它在其他数据结构的范畴里应用很广。 枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式。例如,枚举定义了一个叫nextElement 的方法,该方法用来得到一个包含多元素的数据结构的下一个元素。
Enumeration接口中定义了一些方法,通过这些方法可以枚举(一次获得一个)对象集合中的元素。
这种传统接口已被迭代器取代,虽然Enumeration 还未被遗弃,但在现代代码中已经被很少使用了。尽管如此,它还是使用在诸如Vector和Properties这些传统类所定义的方法中,除此之外,还用在一些API类,并且在应用程序中也广泛被使用。
方法名 | 方法描述 |
---|---|
boolean hasMoreElements( ) | 测试此枚举是否包含更多的元素 |
Object nextElement( ) | 如果此枚举对象至少还有一个可提供的元素,则返回此枚举的下一个元素。 |
package Liu;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.Vector;
public class ArrayMethodDemo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Enumeration <String> days; //声明枚举对象days
Vector<String> daynames = new Vector<String>(); //实例化一个空的字符串向量
daynames.add("Monday");
daynames.add("Tuesday");
daynames.add("Wednesday");
daynames.add("Thursday");
daynames.add("Friday");
daynames.add("Saturday");
daynames.add("Sunday");
days = daynames.elements(); //赋值枚举对象days
while(days.hasMoreElements()) {
System.out.println(days.nextElement());
}
}
}
输出:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
向量(Vector)
向量(Vector)类和传统数组非常相似,但是Vector的大小能根据需要动态的变化。
和数组一样,Vector对象的元素也能通过索引访问。
使用Vector类最主要的好处就是在创建对象的时候不必给对象指定大小,它的大小会根据需要动态的变化。
ector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:
Vector 是同步访问的。
Vector 包含了许多传统的方法,这些方法不属于集合框架。
Vector 主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。
Vector类有4种构造方法
第一种构造方法创建一个默认的向量,默认大小为 10:
Vector()
第二种构造方法创建指定大小的向量。
Vector(int size)
第三种构造方法创建指定大小的向量,并且增量用 incr 指定。增量表示向量每次增加的元素数目。
Vector(int size,int incr)
第四种构造方法创建一个包含集合 c 元素的向量:
Vector(Collection c)
Vector类的主要方法|
方法名 | 方法描述 |
---|---|
void add(int index, Object element) | 在此向量的指定位置插入指定的元素。 |
boolean add(Object o) | 将指定元素添加到此向量的末尾 |
boolean addAll(Collection c) | 将指定 Collection 中的所有元素添加到此向量的末尾,按照指定 collection 的迭代器所返回的顺序添加这些元素。 |
boolean addAll(int index, Collection c) | 在指定位置将指定 Collection 中的所有元素插入到此向量中。 |
void addElement(Object obj) | 将指定的组件添加到此向量的末尾,将其大小增加 1。 |
int capacity() | 返回此向量的当前容量。 |
void clear() | 从此向量中移除所有元素。 |
Object clone() | 返回向量的一个副本。 |
boolean contains(Object elem) | 如果此向量包含指定的元素,则返回 true |
boolean containsAll(Collection c) | 如果此向量包含指定 Collection 中的所有元素,则返回 true。 |
void copyInto(Object[] anArray) | 将此向量的组件复制到指定的数组中。 |
Object elementAt(int index) | 返回指定索引处的组件。 |
Enumeration elements() | 返回此向量的组件的枚举。 |
boolean equals(Object o) | 比较指定对象与此向量的相等性。 |
Object firstElement() | 返回此向量的第一个组件(位于索引 0) 处的项)。 |
Object lastElement() | 返回此向量的最后一个组件。 |
Object get(int index) | 返回向量中指定位置的元素。 |
int indexOf(Object elem) | 返回此向量中第一次出现的指定元素的索引,如果此向量不包含该元素,则返回 -1。 |
void insertElementAt(Object obj, int index) | 将指定对象作为此向量中的组件插入到指定的 index 处。 |
boolean isEmpty() | 测试此向量是否不包含组件。 |
Object remove(int index) | 移除此向量中指定位置的元素。 |
boolean remove(Object o) | 移除此向量中指定元素的第一个匹配项,如果向量不包含该元素,则元素保持不变。 |
boolean removeAll(Collection c) | 从此向量中移除包含在指定 Collection 中的所有元素。 |
void removeAllElements() | 从此向量中移除全部组件,并将其大小设置为零。 |
boolean removeElement(Object obj) | 从此向量中移除变量的第一个(索引最小的)匹配项。 |
void removeElementAt(int index) | 删除指定索引处的组件。 |
protected void removeRange(int fromIndex, int toIndex) | 从此 List 中移除其索引位于 fromIndex(包括)与 toIndex(不包括)之间的所有元素。 |
void setSize(int newSize) | 设置此向量的大小。 |
int size() | 返回此向量中的组件数。 |
void setElementAt(Object obj, int index) | 将此向量指定 index 处的组件设置为指定的对象。 |
String toString() | 返回此向量的字符串表示形式,其中包含每个元素的 String 表示形式。 |
Object[] toArray() | 返回一个数组,包含此向量中以恰当顺序存放的所有元素。 |
package Liu;
import java.util.Enumeration;
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
//initial size = 3,increment = 2
//初始大小为3个单位,不够则以2个单位为增量增加向量大小
Vector vec = new Vector(3,2);
//size()返回当前向量中的组件数
System.out.println("vector's initial size =" + vec.size());
//capacity()返回当前向量的容量
System.out.println("vector's capacity = " + vec.capacity());
vec.addElement(new Integer(1));
vec.addElement(new Integer(2));
vec.addElement(new Integer(3));
vec.addElement(new Integer(4));
vec.addElement(new Integer(5));
vec.addElement(new Integer(6));
System.out.println("Capacity after six additions: " + vec.capacity());
System.out.println("当前向量中第一个组件是:" + vec.firstElement());
vec.add(new Double(2.456));
vec.add(new String("Hello,world!"));
System.out.println("当前向量容量:" + vec.capacity());
System.out.println("当前向量中最后一个组件是:" + vec.lastElement());
if(vec.contains(new Integer(3))) {
System.out.println("当前向量中包含此组件");
}
// enumerate the elements in the vector.
Enumeration vEnum = vec.elements();
System.out.println("Elements in vector:");
while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
}
}
输出:
vector's initial size =0
vector's capacity = 3
Capacity after six additions: 7
当前向量中第一个组件是:1
当前向量容量:9
当前向量中最后一个组件是:Hello,world!
当前向量中包含此组件
Elements in vector:
1 2 3 4 5 6 2.456 Hello,world!
栈(Stack)
栈(Stack)实现了一个后进先出(LIFO)的数据结构。
你可以把栈理解为对象的垂直分布的栈,当你添加一个新元素时,就将新元素放在其他元素的顶部。
当你从栈中取元素的时候,就从栈顶取一个元素。换句话说,最后进栈的元素最先被取出。
Stack 类,栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
堆栈只定义了默认构造函数,用来创建一个空栈。
Stack()
实例
package Liu;
import java.util.Stack;
public class StackDemo {
static void showpush(Stack<Integer> stack,int a) {
//进栈一个整数对象
stack.push(new Integer(a));
//输出
System.out.println("push(" + a + ")");
System.out.println(stack);
}
static void showpop(Stack<Integer> stack) {
//栈顶元素出栈
System.out.print("pop ->");
Integer aInt = stack.pop();
//输出
System.out.println(aInt);
System.out.println(stack);
}
public static void main(String[] args) {
Stack<Integer> s = new Stack<Integer>();
System.out.println("stack:" + s);
showpush(s,12);
showpush(s, 37);
showpop(s);
showpush(s, 520);
}
}
输出
stack:[]
push(12)
[12]
push(37)
[12, 37]
pop ->37
[12]
push(520)
[12, 520]
字典(Dictionary)
字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构。
当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary。
由于Dictionary类是抽象类,所以它只提供了键映射到值的数据结构,而没有提供特定的实现。
Dictionary 类是一个抽象类,用来存储键/值对,作用和Map类相似。
给出键和值,你就可以将值存储在Dictionary对象中。一旦该值被存储,就可以通过它的键来获取它。所以和Map一样, Dictionary 也可以作为一个键/值对列表。
Dictionary定义的抽象方法
方法名 | 方法描述 |
---|---|
Enumeration elements( ) | 返回此 dictionary 中值的枚举。 |
Object get(Object key) | 返回此 dictionary 中该键所映射到的值。 |
boolean isEmpty( ) | 测试此 dictionary 是否不存在从键到值的映射。 |
Enumeration keys( ) | 返回此 dictionary 中的键的枚举。 |
Object put(Object key, Object value) | 将指定 key 映射到此 dictionary 中指定 value。 |
Object remove(Object key) | 从此 dictionary 中移除 key (及其相应的 value)。 |
int size( ) | 返回此 dictionary 中条目(不同键)的数量。 |
Dictionary类已经过时了。在实际开发中,你可以实现Map接口来获取键/值的存储功能
在这里插入代码片