開発メモ/Java/基礎/Hashtableクラス
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
*Hashtableクラス [#j4166cff]
ハッシュ表を使うとオブジェクトの保存と取得が簡単になりま...
ハッシュ表のエントリにはキーと値が含まれます。~
キーは一意でなければなりません。値は一意でなくてもよいで...
インディックスを使って要素を取り出すことはできません。~
キーを使って値を取り出すことはできます。~
このようなデータ構造は連想配列とも呼ばれます。~
''Hashtableのコンストラクタ))
#codeprettify{{
hashtable()
Hashtable(int n)
Hashtable(int n, float lf)
}}
|引数|説明|h
|n|初期サイズ|
|lf|荷重率(load factor:有効なデータが全体のサイズにしめる...
エントリ数と荷重率の積を越えた場合はハッシュ表のサイズが...
''例''
#codeprettify{{
import java.util.*;
class HashtableDemo{
public static void main(String args[]){
//ハッシュ表を作成して情報を設定する
Hashtable hashtable = new Hashtable();
hashtable.put("apple","red");
hashtable.put("strawberry","red");
hashtable.put("lime","green");
hashtable.put("banana","yellow");
hashtable.put("orange","orange");
//ハッシュ表の要素を表示する
Enumeration e = hashtable.keys();
while(e.hasMoreElements()){
Object k = e.nextElement(); //キーを...
Object v = hashtable.get(k); //キーを...
System.out.println("key = " + k + "; value ="...
}
//'appleの値を表示する
System.out.print("\nThe color of apple is: ");
//キー”アップル”に関連づけられた値を得る
Object v=hashtable.get("apple");
System.out.print(v);
}
}
}}
''結果''
key = strawberry; value =red
key = orange; value =orange
key = apple; value =red
key = lime; value =green
key = banana; value =yellow
The color of apple is :red
''例''
#codeprettify{{
import java.util.*;
class Person {
String name, telephone, fax, email;
//Personコンストラクタ
//ここで情報を記憶する
Person(String name, String telephone, String fax, Strin...
this.name = name;
this.telephone = telephone;
this.fax = fax;
this.email = email;
}
//情報を文字列で取り出す
public String toString() {
return name + "; " + telephone + "; " + fax + "; " + ...
}
}
class PeopleHashtable {
public static void main(String args[]) {
// ハッシュ表を作成し,データを設定する
Hashtable hashtable = new Hashtable();
//Person型の変数を用意して、それをhashtableに登録して...
Person p1 = new Person("Susan", "5634", "2343", "sue");
hashtable.put("111111111", p1);
Person p2 = new Person("Claire", "4545", "3331", "cla...
hashtable.put("222222222", p2);
Person p3 = new Person("Kim", "9821", "9899", "kim");
hashtable.put("333333333", p3);
Person p4 = new Person("Viviane", "4689", "2211", "vi...
hashtable.put("444444444", p4);
Person p5 = new Person("Barbara", "1212", "6655", "ba...
hashtable.put("555555555", p5);
// ハッシュ表の要素を表示する
Enumeration e = hashtable.keys();
while(e.hasMoreElements()) {
Object k = e.nextElement();
Object v = hashtable.get(k);
System.out.println("key = " + k + "; value = " + v);
}
}
}
}}
''結果''
c:\>java PeopleHashtable
key = 555555555; value = Barbara; 1212; 6655; barb
key = 333333333; value = kim; 9821; 9899; kim
key = 444444444; value = Viviane; 4689; 2211; viv
key = 111111111; value = Susan; 5634; 2343; sue
key = 222222222; value = Claire; 4545; 3331; claire
終了行:
*Hashtableクラス [#j4166cff]
ハッシュ表を使うとオブジェクトの保存と取得が簡単になりま...
ハッシュ表のエントリにはキーと値が含まれます。~
キーは一意でなければなりません。値は一意でなくてもよいで...
インディックスを使って要素を取り出すことはできません。~
キーを使って値を取り出すことはできます。~
このようなデータ構造は連想配列とも呼ばれます。~
''Hashtableのコンストラクタ))
#codeprettify{{
hashtable()
Hashtable(int n)
Hashtable(int n, float lf)
}}
|引数|説明|h
|n|初期サイズ|
|lf|荷重率(load factor:有効なデータが全体のサイズにしめる...
エントリ数と荷重率の積を越えた場合はハッシュ表のサイズが...
''例''
#codeprettify{{
import java.util.*;
class HashtableDemo{
public static void main(String args[]){
//ハッシュ表を作成して情報を設定する
Hashtable hashtable = new Hashtable();
hashtable.put("apple","red");
hashtable.put("strawberry","red");
hashtable.put("lime","green");
hashtable.put("banana","yellow");
hashtable.put("orange","orange");
//ハッシュ表の要素を表示する
Enumeration e = hashtable.keys();
while(e.hasMoreElements()){
Object k = e.nextElement(); //キーを...
Object v = hashtable.get(k); //キーを...
System.out.println("key = " + k + "; value ="...
}
//'appleの値を表示する
System.out.print("\nThe color of apple is: ");
//キー”アップル”に関連づけられた値を得る
Object v=hashtable.get("apple");
System.out.print(v);
}
}
}}
''結果''
key = strawberry; value =red
key = orange; value =orange
key = apple; value =red
key = lime; value =green
key = banana; value =yellow
The color of apple is :red
''例''
#codeprettify{{
import java.util.*;
class Person {
String name, telephone, fax, email;
//Personコンストラクタ
//ここで情報を記憶する
Person(String name, String telephone, String fax, Strin...
this.name = name;
this.telephone = telephone;
this.fax = fax;
this.email = email;
}
//情報を文字列で取り出す
public String toString() {
return name + "; " + telephone + "; " + fax + "; " + ...
}
}
class PeopleHashtable {
public static void main(String args[]) {
// ハッシュ表を作成し,データを設定する
Hashtable hashtable = new Hashtable();
//Person型の変数を用意して、それをhashtableに登録して...
Person p1 = new Person("Susan", "5634", "2343", "sue");
hashtable.put("111111111", p1);
Person p2 = new Person("Claire", "4545", "3331", "cla...
hashtable.put("222222222", p2);
Person p3 = new Person("Kim", "9821", "9899", "kim");
hashtable.put("333333333", p3);
Person p4 = new Person("Viviane", "4689", "2211", "vi...
hashtable.put("444444444", p4);
Person p5 = new Person("Barbara", "1212", "6655", "ba...
hashtable.put("555555555", p5);
// ハッシュ表の要素を表示する
Enumeration e = hashtable.keys();
while(e.hasMoreElements()) {
Object k = e.nextElement();
Object v = hashtable.get(k);
System.out.println("key = " + k + "; value = " + v);
}
}
}
}}
''結果''
c:\>java PeopleHashtable
key = 555555555; value = Barbara; 1212; 6655; barb
key = 333333333; value = kim; 9821; 9899; kim
key = 444444444; value = Viviane; 4689; 2211; viv
key = 111111111; value = Susan; 5634; 2343; sue
key = 222222222; value = Claire; 4545; 3331; claire
ページ名: