>  기사  >  Java  >  자바 사전

자바 사전

WBOY
WBOY원래의
2024-08-30 15:40:34779검색

Java 사전에서 util.Dictionary는 키-값 저장소 저장소를 나타내고 맵처럼 작동하는 추상 클래스입니다. 키와 일부 값이 제공되면 값은 사전 객체에 저장될 수 있습니다. 값을 저장한 후 키를 사용하여 검색할 수 있습니다. 지도와의 이러한 유사성은 사전 클래스가 종종 유사하게 작동한다고 언급되는 이유입니다. 후속 섹션에서는 사전 클래스의 생성자, 선언 및 추가 세부 정보를 다룹니다.

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

선언

 아래는 Dictionary 클래스 선언입니다.

public abstract class Dictionary extends object

건축자

다음은 Dictionary 클래스의 유일한 생성자입니다.

Dictionary() : 단일 생성자.

Java Dictionary 수업은 어떻게 진행되나요?

위에서 설명한 것처럼 사전 클래스는 지도와 유사하게 동작하는 추상 클래스입니다. 특정 키와 값을 제공하여 사전 개체에 값을 저장할 수 있습니다. 값을 저장한 후 키를 사용하여 검색할 수 있습니다. 이 클래스에서는 null이 아닌 모든 키와 값을 사용할 수 있습니다.

Java 사전 클래스 메소드

사전 클래스의 다양한 방식을 살펴보겠습니다.

요소()

사전은 사용 가능한 값의 열거를 반환합니다.

구문:

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

출력:

자바 사전

두 개의 요소가 사전에 추가되며 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());
}
}
}

출력:

자바 사전

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

출력:

자바 사전

두 개의 요소를 사전에 추가한 후 제거() 메서드를 사용하여 그 중 하나를 제거할 수 있습니다.

키()

사전에서 사용 가능한 키에 대해 열거형이 반환됩니다.

구문:

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

출력:

자바 사전

두 개의 요소가 사전에 추가되며 키() 메서드를 사용하여 검색할 수 있습니다.

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

출력:

자바 사전

사전에 키-값 쌍이 있는 경우 isEmpty() 메서드를 호출하면 false가 반환됩니다.

get(객체 키)

사전은 지정된 키에 해당하는 값을 반환합니다.

구문:

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

출력:

자바 사전

두 개의 요소를 사전에 추가한 후 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());
}
}

출력:

자바 사전

사전의 크기를 확인하려면 요소 제거 전후에 size() 메서드를 활용할 수 있습니다.

결론

이 글에서는 Dictionary 클래스의 선언, 생성자, 작업, 메소드 등 여러 측면을 예제와 함께 자세히 설명합니다.

위 내용은 자바 사전의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:자바 저장소다음 기사:자바 저장소