2013年9月22日星期日

Trouble you have any questions look at this code ( very simple Hashtable traversal )

public class TestHashtable {
public static void main(String[] args)
{
String key,value;
Hashtable<String,String> ht = new Hashtable<String,String>();
ht.put("xixi", "haha");
ht.put("hello", "world");
ht.put("ni", "hao");
Set<Map.Entry<String, String>> s = ht.entrySet();
Iterator<Map.Entry<String,String>> it = s.iterator();
while( it.hasNext() )
{
key = it.next().getKey();
value = it.next().getValue();
System.out.println("key:"+key+"value:"+value);
}
}
}

Results traverse the result is :
key: xixivalue: hao
Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator
at java.util.Hashtable $ Enumerator.nextElement (Hashtable.java: 1021)
at java.util.Hashtable $ Enumerator.next (Hashtable.java: 1032)
at TestHashtable.TestHashtable.main (TestHashtable.java: 21)
really can not find any reason for it, and use keySet traverse is the same. Find Great God enlighten you !
------ Solution ---------------------------------------- ----
should be
value = ht.get (key); fishes ah
------ For reference only ------------------- --------------------

keySet that is the way it is used entrySet, are the same, the output is wrong.
------ For reference only -------------------------------------- -

keySet that is the way it is used entrySet, are the same, the output is wrong.   how could be wrong, look at your application is not util class bar.
------ For reference only -------------------------------------- -

keySet that is the way it is used entrySet, are the same, the output is wrong.        how could be wrong, look at your application is not util class bar.  
Oh yes , all util class
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
------ For reference only -------------------------- -------------

keySet that is the way it is used entrySet, are the same, the output is wrong.        how could be wrong, look at your application is not util class bar.  
You do not believe can go to the eclipse try. . .
------ For reference only -------------------------------------- -
key = it.next () . getKey ();
value = it.next () . getValue ();

you which is equivalent to two iterations feasible , with a key
------ For reference only ---------------------- -----------------
or changed
Entry entry = it.next ();
key = entry.getKey ();
value = entry.getValue ();


good look it.next () mean.
------ For reference only -------------------------------------- -

this is true , negligence , and actually make this mistake , thank you very much , Java novice, No wonder, then wonder, then , hey .

没有评论:

发表评论