JAVA里面的dictionary<t,t>怎么用; 举个简单的例子最好,
2个回答
展开全部
我是用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);
}
}
这一段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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
dictionary<t,t> 是个抽象类,具体实现要由它的子类实现,比如Hashtable,实现键值对功能。
下面这个小例子,实现存和读取功能
Dictionary<String,String> hashTable=new Hashtable<String,String>();
hashTable.put("s1", "baidu");
hashTable.put("s2", "google");
String str=hashTable.get("s1");
System.out.println(str);
下面这个小例子,实现存和读取功能
Dictionary<String,String> hashTable=new Hashtable<String,String>();
hashTable.put("s1", "baidu");
hashTable.put("s2", "google");
String str=hashTable.get("s1");
System.out.println(str);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询