Hashtable的使用
//Hashtable非常方便的被用来储存对象 任何对象均可以放入 有一个唯一的key与之对应:
String[] array1 = {"A","B","C"};
String[] array2 = {"X","Y","Z"};
String[] array3 = {"1","2","3"};
Hashtable arrays = new Hashtable();
arrays.put("Array1", array1);
arrays.put("Array2", array2);
arrays.put("Array3", array3);
String[] resultArray = (String[])(arrays.get("Array2"));
System.out.println(resultArray[0] + ", " + resultArray[1] + ", " + resultArray[2]);
//以下是遍历Hashtable:
Enumeration keys = hash.keys();
while( keys.hasMoreElements() )
{
Object key = keys.nextElement();
Object value = hash.get(key);
}
//-------------------------------
Hashtable hashTable = new Hashtable();
hashTable.Add("hunan","changsha");
hashTable.Add("beijing","beijing");
hashTable.Add("anhui","hefei");
hashTable.Add("sichuan","chengdu");
foreach(string str in hashTable.Keys)
{
Console.WriteLine(str + " : " + hashTable[str]);
}
//Hashtable非常方便的被用来储存对象 任何对象均可以放入 有一个唯一的key与之对应:
String[] array1 = {"A","B","C"};
String[] array2 = {"X","Y","Z"};
String[] array3 = {"1","2","3"};
Hashtable arrays = new Hashtable();
arrays.put("Array1", array1);
arrays.put("Array2", array2);
arrays.put("Array3", array3);
String[] resultArray = (String[])(arrays.get("Array2"));
System.out.println(resultArray[0] + ", " + resultArray[1] + ", " + resultArray[2]);
//以下是遍历Hashtable:
Enumeration keys = hash.keys();
while( keys.hasMoreElements() )
{
Object key = keys.nextElement();
Object value = hash.get(key);
}
//-------------------------------
Hashtable hashTable = new Hashtable();
hashTable.Add("hunan","changsha");
hashTable.Add("beijing","beijing");
hashTable.Add("anhui","hefei");
hashTable.Add("sichuan","chengdu");
foreach(string str in hashTable.Keys)
{
Console.WriteLine(str + " : " + hashTable[str]);
}