首頁  >  文章  >  Java  >  Java字典

Java字典

WBOY
WBOY原創
2024-08-30 15:40:34779瀏覽

在 java Dictionary 中,util.Dictionary 是抽象類,表示鍵值儲存庫,其行為類似於地圖。如果給予一個鍵和一些值,則值可以儲存在字典物件中。儲存值後,可以使用密鑰檢索該值。與地圖的這種相似性就是為什麼字典類通常被稱為功能相似的原因。後續部分將介紹字典類別的建構函數、聲明和其他詳細資訊。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

聲明

 以下是字典類別的陳述。

public abstract class Dictionary extends object

建構子

以下是Dictionary類別的唯一建構子。

Dictionary() : 唯一建構子。

Java Dictionary 類別如何運作?

如上所述,字典類是一個抽象類,其行為與地圖類似。透過提供特定的鍵和值,您可以將值保存在字典物件中。儲存值後,可以使用密鑰檢索該值。您可以在此類中使用任何非空鍵和值。

Java 字典類別方法

讓我們來看看Dictionary類別的不同方法。

元素()

字典將傳回其中可用值的枚舉。

文法:

public abstract Enumeration elements()

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
<strong>/</strong>/ Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}   }
}

輸出:

Java字典

兩個元素被加入到字典中,您可以使用 elements() 方法檢索這些鍵的值。

put(K 鍵, V 值)

提到的鍵將會對應到給定的值。

文法:

public abstract V put(K key, V value)

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
<strong>/</strong>/ Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
}
}

輸出:

Java字典

使用 put() 方法將兩個元素加入字典中,稍後再檢索這些鍵的值。

刪除(物件鍵)

鍵和對應的值將從字典中刪除。

文法:

public abstract V remove(Object key)

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
// remove the element 99 using remove() method
System.out.println(" Remove the element : " + dict.remove("99"));
// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary after removal: " + e.nextElement());
}
}
}

輸出:

Java字典

新增兩個元素後,可以使用remove()方法刪除其中一個。

按鍵()

將傳回字典中可用鍵的枚舉。

文法:

public abstract Enumeration keys()

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Return the enumeration of dictionary using elements() method
for (Enumeration e = dict.keys(); e.hasMoreElements();)
{
System.out.println("Keys available in the dictionary : " + e.nextElement());
}
}
}

輸出:

Java字典

兩個元素被加入到字典中,並且可以使用keys()方法檢索。

isEmpty()

檢查字典是否沒有映射鍵值。如果沒有關係,則傳回true。否則,錯誤。

文法:

public abstract booleanisEmpty()

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("101", "Anna");
dict.put("202", "Adam");
// Checks no key-value pairs
System.out.println("Is there any no key-value pair : " + dict.isEmpty() + " \n " );
}
}

輸出:

Java字典

當字典中存在鍵值對時,呼叫 isEmpty() 方法將會傳回 false。

取得(物件鍵)

字典將傳回與指定鍵對應的值。

文法:

public abstract V get(Object key)

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
// Dictionary initialization
Dictionary dict = new Hashtable();
// Map the keys to the values given using put() method
dict.put("99", "Sarah");
dict.put("82", "Elsa");
<strong> </strong>// Return the eneumeration of dictionary using elements() method
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
System.out.println(" Remove the element : " + dict.remove("99"));
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary after removal: " + e.nextElement());
}
System.out.println("The value of the key 82 is : " + dict.get("82"));
}
}

輸出:

Java字典

在字典中新增兩個元素後,您可以使用 get() 方法檢索其中一個元素。

大小()

將傳回條目數,可在字典中查看。

文法:

public abstract intsize()

範例:

import java.util.*;
public class DictionaryExample
{
public static void main(String[] args)
{
Dictionary dict = new Hashtable();
dict.put("99", "Sarah");
dict.put("82", "Elsa");
for (Enumeration e = dict.elements(); e.hasMoreElements();)
{
System.out.println("Values available in the dictionary : " + e.nextElement());
}
System.out.println("Dictionary size before removal of 99 is : " + dict.size());
// remove the element 99 using remove() method
System.out.println(" Remove the element : " + dict.remove("99"));
System.out.println("Dictionary size after removal of 99 is : " + dict.size());
}
}

輸出:

Java字典

要確定字典的大小,您可以在刪除元素之前和之後使用 size() 方法。

結論

本文透過範例詳細解釋了 Dictionary 類別的宣告、建構子、工作方式和方法等幾個面向。

以上是Java字典的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:Java儲存庫下一篇:Java儲存庫