Java数据结构

来源:互联网 发布:为什么淘宝5星好评 编辑:程序博客网 时间:2024/05/22 11:29

Java 数据结构


Java工具包提供了强大的数据结构。在Java中的数据结构主要包括以下几种接口和类:
枚举(Enumeration)
位集合(BitSet)
向量(Vector)
栈(Stack)
字典(Dictionary)
哈希表(Hashtable)
属性(Properties)

枚举(Enumeration)
枚举(The Enumeration)接口定义了一种从数据结构中取回连续元素的方式。

import java.util.Vector;import java.util.Enumeration;public class EnumerationTester {   public static void main(String args[]) {      Enumeration days;      Vector dayNames = new Vector();      dayNames.add("Sunday");      dayNames.add("Monday");      dayNames.add("Tuesday");      dayNames.add("Wednesday");      dayNames.add("Thursday");      dayNames.add("Friday");      dayNames.add("Saturday");      days = dayNames.elements();      while (days.hasMoreElements()){         System.out.println(days.nextElement());       }   }}

位集合(BitSet)

不常用(此处略)

向量(Vector)

Vector类实现了一个动态数组。和ArrayList和相似,但是两者是不同的:
Vector是同步访问的。
Vector包含了许多传统的方法,这些方法不属于集合框架。
Vector主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。
Vector类支持4种构造方法。
第一种构造方法创建一个默认的向量,默认大小为10:
Vector()
第二种构造方法创建指定大小的向量。
Vector(int size)
第三种构造方法创建指定大小的向量,并且增量用incr指定. 增量表示向量每次增加的元素数目。
Vector(int size,int incr)
第四中构造方法创建一个包含集合c元素的向量:
Vector(Collection c)

import java.util.*;public class VectorDemo {   public static void main(String args[]) {      // initial size is 3, increment is 2      Vector v = new Vector(3, 2);      System.out.println("Initial size: " + v.size());      System.out.println("Initial capacity: " +      v.capacity());      v.addElement(new Integer(1));      v.addElement(new Integer(2));      v.addElement(new Integer(3));      v.addElement(new Integer(4));      System.out.println("Capacity after four additions: " +          v.capacity());      v.addElement(new Double(5.45));      System.out.println("Current capacity: " +      v.capacity());      v.addElement(new Double(6.08));      v.addElement(new Integer(7));      System.out.println("Current capacity: " +      v.capacity());      v.addElement(new Float(9.4));      v.addElement(new Integer(10));      System.out.println("Current capacity: " +      v.capacity());      v.addElement(new Integer(11));      v.addElement(new Integer(12));      System.out.println("First element: " +         (Integer)v.firstElement());      System.out.println("Last element: " +         (Integer)v.lastElement());      if(v.contains(new Integer(3)))         System.out.println("Vector contains 3.");      // enumerate the elements in the vector.      Enumeration vEnum = v.elements();      System.out.println("\nElements in vector:");      while(vEnum.hasMoreElements())         System.out.print(vEnum.nextElement() + " ");      System.out.println();   }}

栈(Stack)

栈是Vector的一个子类,它实现了一个标准的后进先出的栈。
堆栈只定义了默认构造函数,用来创建一个空栈。 堆栈除了包括Vector定义的所有方法,也定义了自己的一些方法。
序号 方法描述
1 boolean empty()
测试堆栈是否为空。
2 Object peek( )
查看堆栈顶部的对象,但不从堆栈中移除它。
3 Object pop( )
移除堆栈顶部的对象,并作为此函数的值返回该对象。
4 Object push(Object element)
把项压入堆栈顶部。
5 int search(Object element)
返回对象在堆栈中的位置,以 1 为基数。

import java.util.*;public class StackDemo {   static void showpush(Stack st, int a) {      st.push(new Integer(a));      System.out.println("push(" + a + ")");      System.out.println("stack: " + st);   }   static void showpop(Stack st) {      System.out.print("pop -> ");      Integer a = (Integer) st.pop();      System.out.println(a);      System.out.println("stack: " + st);   }   public static void main(String args[]) {      Stack st = new Stack();      System.out.println("stack: " + st);      showpush(st, 42);      showpush(st, 66);      showpush(st, 99);      showpop(st);      showpop(st);      showpop(st);      try {         showpop(st);      } catch (EmptyStackException e) {         System.out.println("empty stack");      }   }}

以上实例编译运行结果如下:

stack: [ ]
push(42)
stack: [42]
push(66)
stack: [42, 66]
push(99)
stack: [42, 66, 99]
pop -> 99
stack: [42, 66]
pop -> 66
stack: [42]
pop -> 42
stack: [ ]
pop -> empty stack

字典(Dictionary)

Dictionary类已经过时了。在实际开发中,你可以实现Map接口来获取键/值的存储功能。

哈希表(Hashtable)

java 2 重构的Hashtable实现了Map接口,因此,Hashtable现在集成到了集合框架中。它和HashMap类很相似,但是它支持同步。
像HashMap一样,Hashtable在哈希表中存储键/值对。当使用一个哈希表,要指定用作键的对象,以及要链接到该键的值。
然后,该键经过哈希处理,所得到的散列码被用作存储在该表中值的索引。

import java.util.*;public class HashTableDemo {   public static void main(String args[]) {      // Create a hash map      Hashtable balance = new Hashtable();      Enumeration names;      String str;      double bal;      balance.put("Zara", new Double(3434.34));      balance.put("Mahnaz", new Double(123.22));      balance.put("Ayan", new Double(1378.00));      balance.put("Daisy", new Double(99.22));      balance.put("Qadir", new Double(-19.08));      // Show all balances in hash table.      names = balance.keys();      while(names.hasMoreElements()) {         str = (String) names.nextElement();         System.out.println(str + ": " +         balance.get(str));      }      System.out.println();      // Deposit 1,000 into Zara's account      bal = ((Double)balance.get("Zara")).doubleValue();      balance.put("Zara", new Double(bal+1000));      System.out.println("Zara's new balance: " +      balance.get("Zara"));   }}

属性(Properties)

Properties 继承于 Hashtable.表示一个持久的属性集.属性列表中每个键及其对应值都是一个字符串。
Properties 类被许多Java类使用。例如,在获取环境变量时它就作为System.getProperties()方法的返回值。

import java.util.*;public class PropDemo {   public static void main(String args[]) {      Properties capitals = new Properties();      Set states;      String str;      capitals.put("Illinois", "Springfield");      capitals.put("Missouri", "Jefferson City");      capitals.put("Washington", "Olympia");      capitals.put("California", "Sacramento");      capitals.put("Indiana", "Indianapolis");      // Show all states and capitals in hashtable.      states = capitals.keySet(); // get set-view of keys      Iterator itr = states.iterator();      while(itr.hasNext()) {         str = (String) itr.next();         System.out.println("The capital of " +            str + " is " + capitals.getProperty(str) + ".");      }      System.out.println();      // look for state not in list -- specify default      str = capitals.getProperty("Florida", "Not Found");      System.out.println("The capital of Florida is "          + str + ".");   }}

以上实例编译运行结果如下:

The capital of Missouri is Jefferson City.
The capital of Illinois is Springfield.
The capital of Indiana is Indianapolis.
The capital of California is Sacramento.
The capital of Washington is Olympia.

The capital of Florida is Not Found.

0 0