如何将元素插入字典? [英] How do I insert an element into a dictionary?

查看:558
本文介绍了如何将元素插入字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将元素插入字典?

推荐答案

如果他们提供了一个例子...... [ ^ ]。


A 字典< TKey,TValue> 并不意味着按特定顺序对其项目进行排序。相反,它使用一个键作为快速方式(Big-O表示法中的O(n))来检索特定项目。



在给定位置插入一个项目是一个列表的功能。



也许如果你更准确地描述你的问题我们可以告诉你一些解决方法。

我们需要知道为什么你需要插入,即导致这个要求的实现细节。
A Dictionary<TKey, TValue> is not meant to sort its items in a specific order. Instead, it uses a key as a quick way (O(n) in Big-O notation) to retrieve a specific item.

Inserting an item at a given position is a list's feature.

Maybe if you describe your issue more precisely we could show you some workarounds.
We would need to know why you need insertion, i.e. the details of your implementation that lead to this requirement.


这是在Dictionary中添加元素的方法之一,

Dictionary< string ,INT> dict = new Dictionary< string,int>();

dict.Add(bat,1);

dict.Add(Ball,2);

dict.Add(Stump,3);



这是从Dictionary中检索元素的方法之一,


foreach(词典<字符串,int> dict中的值)

{

console.WriteLine(Key is { 0}和值为{1},dict.Key,dict.Value);

}
This is one of the way to add element in Dictionary,
Dictionary<string,int> dict = new Dictionary<string,int>();
dict.Add("Bat",1);
dict.Add("Ball",2);
dict.Add("Stump",3);

This is one of the way to retrieve element from Dictionary,

foreach(Dictionary<string,int> values in dict)
{
console.WriteLine("Key is {0} and value is {1}", dict.Key,dict.Value);
}


这篇关于如何将元素插入字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆