1

私はこのようなハッシュテーブルを持っています:

Hashtable<String, String> ht = new Hashtable<String, String>();
 ht.put("A", "one");
 ht.put("B", "two");

次に、そのvalues()メソッドを呼び出してその値を取得し、次のように Collection オブジェクトに格納します。

Collection<String> col = ht.values();

このコレクション オブジェクトには次の値があります。

 one, two.

それから私は電話col.add(" three");しました今回はこのエラーが発生しました:

Exception in thread "main" java.lang.UnsupportedOperationException.

APIを確認しました:

If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false)

remove()コレクションに追加した値 ("three") は一意であり、重複していませんclear()

を呼び出すadd()ことができません。追加できないのはなぜですか?

4

2 に答える 2

0

上記の回答に加えて、Hashtable にはキーと値のペアが必要です。キーではなく値を追加しているだけなので、例外がスローされます。

于 2012-06-05T06:56:44.023 に答える