Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 雜項

Java APIs 和框架

Java 類引用

Java 有用資源

Java - 資料結構



Java 實用程式包 提供的資料結構非常強大,並執行各種功能。這些資料結構包括以下介面和類:

  • 列舉
  • BitSet
  • Vector
  • Stack
  • Dictionary
  • Hashtable
  • Properties

所有這些類現在都是遺留的,Java-2 引入了一個名為 Collections Framework 的新框架,這將在下一章中討論。

列舉

Enumeration 介面 本身不是資料結構,但在其他資料結構的上下文中非常重要。Enumeration 介面定義了一種從資料結構中檢索連續元素的方法。

例如,Enumeration 定義了一個名為 nextElement 的方法,用於獲取包含多個元素的資料結構中的下一個元素。

示例

以下是一個顯示 Vector 列舉用法的示例。

import java.util.Vector;
import java.util.Enumeration;

public class EnumerationTester {

   public static void main(String args[]) {
      Enumeration<String> days;
      Vector<String> 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()); 
      }
   }
}

輸出

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

要詳細瞭解此介面,請檢視 列舉

BitSet

BitSet 類 實現了一組可以單獨設定和清除的位或標誌。

當您需要跟進一組布林值時,此類非常有用;您只需為每個值分配一位,並根據需要設定或清除它。

示例

以下程式說明了 BitSet 資料結構支援的幾種方法。

import java.util.BitSet;
public class BitSetDemo {

  public static void main(String args[]) {
      BitSet bits1 = new BitSet(16);
      BitSet bits2 = new BitSet(16);
      
      // set some bits
      for(int i = 0; i < 16; i++) {
         if((i % 2) == 0) bits1.set(i);
         if((i % 5) != 0) bits2.set(i);
      }
     
      System.out.println("Initial pattern in bits1: ");
      System.out.println(bits1);
      System.out.println("\nInitial pattern in bits2: ");
      System.out.println(bits2);

      // AND bits
      bits2.and(bits1);
      System.out.println("\nbits2 AND bits1: ");
      System.out.println(bits2);

      // OR bits
      bits2.or(bits1);
      System.out.println("\nbits2 OR bits1: ");
      System.out.println(bits2);

      // XOR bits
      bits2.xor(bits1);
      System.out.println("\nbits2 XOR bits1: ");
      System.out.println(bits2);
   }
}

輸出

Initial pattern in bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

Initial pattern in bits2:
{1, 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, 14}

bits2 AND bits1:
{2, 4, 6, 8, 12, 14}

bits2 OR bits1:
{0, 2, 4, 6, 8, 10, 12, 14}

bits2 XOR bits1:
{}

Vector

Vector 類 類似於傳統的 Java 陣列,不同之處在於它可以根據需要增長以容納新元素。

與陣列一樣,Vector 物件的元素可以透過向量中的索引訪問。

使用 Vector 類的好處是,您不必擔心在建立時將其設定為特定大小;它會在需要時自動收縮和增長。

示例

以下程式說明了 Vector 集合支援的幾種方法。

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();
   }
}

輸出

Initial size: 0
Initial capacity: 3
Capacity after four additions: 5
Current capacity: 5
Current capacity: 7
Current capacity: 9
First element: 1
Last element: 12
Vector contains 3.

Elements in vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

Stack

Stack 類實現元素的後進先出 (LIFO) 堆疊。

您可以將堆疊從字面上理解為物件的垂直堆疊;當您新增新元素時,它會堆疊在其他元素之上。

當您從堆疊中取出一個元素時,它會從頂部取出。換句話說,您新增到堆疊的最後一個元素是第一個被取出的元素。

示例

以下程式說明了 Stack 集合支援的幾種方法。

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 類是一個抽象類,它定義了將鍵對映到值的資料結構。

這在您希望能夠透過特定鍵而不是整數索引訪問資料的情況下很有用。

由於 Dictionary 類是抽象的,因此它只提供鍵對映資料結構的框架,而不是具體的實現。

示例

以下示例顯示了 Java Dictionary keys() 方法的用法。我們使用 Integer、Integer 的 Hashtable 物件建立字典例項。然後我們向其中添加了一些元素。使用 keys() 方法檢索列舉,然後迭代列舉以列印字典的鍵。

package com.tutorialspoint;

import java.util.Enumeration;
import java.util.Dictionary;
import java.util.Hashtable;

public class DictionaryDemo {
   public static void main(String[] args) {

      // create a new hashtable
      Dictionary<Integer, Integer> dictionary = new Hashtable<>();

      // add 2 elements
      dictionary.put(1, 1);
      dictionary.put(2, 2);

      Enumeration<Integer> enumeration = dictionary.keys();

      while(enumeration.hasMoreElements()) {
         System.out.println(enumeration.nextElement());
      }
   }
}

輸出

2
1

Hashtable

Hashtable 類提供了一種根據某些使用者定義的鍵結構組織資料的方法。

例如,在地址列表雜湊表中,您可以根據郵政編碼等鍵而不是人的姓名來儲存和排序資料。

雜湊表中鍵的具體含義完全取決於雜湊表的用途及其包含的資料。

示例

以下示例演示瞭如何使用 Java Hashtable 的 contains() 方法來檢查雜湊表中是否存在某個值。我們建立了一個 Integer,Integer 型別的 Hashtable 物件。然後添加了一些條目,打印表格,並使用 contains() 方法檢查表格中的兩個值。

package com.tutorialspoint;

import java.util.Hashtable;

public class HashtableDemo {
   public static void main(String args[]) {
      
      // create hash table
      Hashtable<Integer,Integer> hashtable = new Hashtable<>();

      // populate hash table
      hashtable.put(1, 1);
      hashtable.put(2, 2);
      hashtable.put(3, 3); 

      System.out.println("Initial table elements: " + hashtable);
      System.out.println("Hashtable contains 2 as value: " + hashtable.contains(2));
      System.out.println("Hashtable contains 4 as value: " + hashtable.contains(4));
   }    
}

輸出

Initial table elements: {3=3, 2=2, 1=1}
Hashtable contains 2 as value: true
Hashtable contains 4 as value: false

屬性

Properties 是 Hashtable 的一個子類。它用於維護值列表,其中鍵是字串,值也是字串。

許多其他 Java 類都使用 Properties 類。例如,在獲取環境值時,System.getProperties() 返回的物件就是這種型別。

示例

以下示例演示瞭如何使用 Java Properties 的 getProperty(String key) 方法根據鍵從 Properties 中獲取值。我們建立了一個 Properties 物件。然後添加了一些條目。使用 getProperty() 方法檢索並列印值。

package com.tutorialspoint;

import java.util.Properties;

public class PropertiesDemo {
   public static void main(String[] args) {
      Properties properties = new Properties();

      //populate properties object
      properties.put("1", "tutorials");
      properties.put("2", "point");
      properties.put("3", "is best");

      System.out.println("Properties elements: " + properties);
      System.out.println("Value: " + properties.getProperty("1"));
   }
}

輸出

Properties elements: {1=tutorials, 2=point, 3=is best}
Value: tutorials
廣告