JAVA里面的dictionary<t,t>怎么用; 举个简单的例子最好,
发布网友
发布时间:2022-05-15 17:39
我来回答
共2个回答
热心网友
时间:2022-04-27 15:37
我是用C#语言的,字典的基本思路就是,键值对!每个键值对应一个值。
这一段C#代码你可以参考一下:我想对你有帮助
public class OthelloCast : List<string>
{
// Use a dictionary to contain
// cast names (key) and actor names (value).
public Dictionary<string, string> OthelloDict =
new Dictionary<string, string>();
public OthelloCast()
{
// Add data to the dictionary.
OthelloDict.Add("Bianca", "Gretchen Rivas");
OthelloDict.Add("Brabantio", "Carlos Lacerda");
OthelloDict.Add("Cassio", "Steve Masters");
OthelloDict.Add("Clown", "Michael Ludwig");
OthelloDict.Add("Desdemona", "Catherine Autier Miconi");
OthelloDict.Add("Duke of Venice", "Ken Circeo");
OthelloDict.Add("Emilia", "Eva Valverde");
OthelloDict.Add("Gratiano", "Akos Kozari");
OthelloDict.Add("Iago", "Darius Stasevicius");
OthelloDict.Add("Lodovico", "Fernando Souza");
OthelloDict.Add("Montano", "Jeff Hay");
OthelloDict.Add("Othello", "Marco Tanara");
OthelloDict.Add("Roderigo", "Pedro Ruivo");
// Populate the list with character names.
foreach (KeyValuePair<string, string> kvp in OthelloDict)
{
this.Add(kvp.Key);
}
}
参考资料:msdn
热心网友
时间:2022-04-27 16:55
dictionary<t,t> 是个抽象类,具体实现要由它的子类实现,比如Hashtable,实现键值对功能。
下面这个小例子,实现存和读取功能
Dictionary<String,String> hashTable=new Hashtable<String,String>();
hashTable.put("s1", "");
hashTable.put("s2", "google");
String str=hashTable.get("s1");
System.out.println(str);