03集合:List<T>,Dictionary<K,V>

List<T>泛型集合

List<T>是C#中一种快捷、易于使用的泛型集合类型,使用泛型编程为编写面向对象程序增加了极大的效率和灵活性。

1、List<T>用法

(1)创建一个List<T>泛型集合对象实例

List<T> list = new List<T>();// T 为列表中元素的类型

List<string> mlist = new List<string>();//创建了一个 string 类型的集合

(2)以一个集合作为参数创建新的泛型集合对象实例

string[] strArr ={"1","2","3","4","5"};
List<string> strList =List<string>(strArr);//把数组作为参数新建一个string类型集合

2、添加元素到List<T>

(1)添加一个元素

strList.Add("Hello");//把一个字符串添加到strList集合中

(2)添加一组元素集合

string[] strArr ={"1","2","3","4","5"};
strList.AddRange(strArr);//把数组添加到集合中,数组也可以理解为一个有限长度的集合

(3)在指定位置添加要添加的元素

Insert(int index, T item);//index 为要添加元素的位置

以下示例:

strList.Insert(1,"萨瓦迪卡");//在集合中第1个元素后面的位置插入一个元素

3、泛型集合的遍历

可以使用 foreach 循环轻松遍历 List<T> 中的所有元素,示例:

foreach(string s in strList)
{
    Console.WriteLine(s);//输出遍历到的元素的值
}

Dictionary<K , V>  键值对集合

下面是对于Dictionary的一些常用的操作,包括遍历,添加,删除等

(1)新建一个Dictionary<Keys ,Values>  键值对集合

//新建一个键是int类型 ,值是 string类型的键值对集合
Dictionary<int,string> dictionary =newDictionary<int,string>();

(2)往键值对集合添加数据

//注意:这里的键可以是声明类型的范围的任意值,不必按照顺序,会跟stirng类型的值相对应
dictionary.Add(1,"张三");
dictionary.Add(21,"李四");
dictionary.Add(33,"王五");
dictionary.Add(4,"赵六");

(3)遍历键值对集合的Keys(键)和Values(值)

foreach(KeyValuePair<int,string> kvp in dictionary)
{
    //输出遍历到的键值对集合元素的键和值
    Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
}

(4)遍历Keys(键)

foreach(int j in dictionary.Keys)
{
    //输出遍历到的键值对元素的键
    Console.WriteLine("key={0},value={1}", j, dictionary[j]);
}

(5)遍历Values(值)

//遍历Values
foreach(string item in dictionary.Values)
{
    //输出遍历到的Values(值)
    Console.WriteLine("value:{0}", item);
}

(5)判断键值对集合是否包含某个Keys(键)

//判断是否包含 2 这个键,返回bool类型,true则包含
if(dictionary.ContainsKey(2))
{
    //输出这个键所对应的值
    Console.WriteLine(dict[2]);
}
时间: 2024-08-25 13:49:49

03集合:List<T>,Dictionary<K,V>的相关文章

C#泛型集合之Dictionary&lt;k, v&gt;使用技巧

1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成 2).任何键都必须是唯一的 3).键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值 4).Key和Value可以是任何类型(string,int,custom class 等) 3.创建及初始化 Dictionary<

C#泛型集合—Dictionary&lt;K,V&gt;使用技巧

[csharp] view plaincopy 1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述  1).从一组键(Key)到一组值(Value)的映射,每一个添加项都是由一个值及其相关连的键组成  2).任何键都必须是唯一的  3).键不能为空引用null(VB中的Nothing),若值为引用类型,则可以为空值  4).Key和Value可以是任何类型(string,int,custom c

C#入门Dictionary&lt;k,v&gt;泛型集合

关于Dictionary<k,v>泛型集合 Dictionary<k,v>通常成为字典,<k,v>约束集合中元素类型: 编译时检查类型约束,无需装箱拆箱操作,与哈希表操作类似: 1 static void Main(string[] args) 2 { 3 //创建几个学员对象 4 Student objStudent1 = new Student(1001, "小明"); 5 Student objStudent2 = new Student(10

遍历Dictionary&lt;K,V&gt;的两种方式

添加 Dictionary<string,int> things = new Dictionary<string,int>(); things.Add(........); things.Add(........); 第一种方式:可以使用Keys和values属性迭代集合中的键和值: foreach(string key in things.Keys) { // ...... } //或 foreach(int value in things.Values) { //.... }

基础才是重中之重~Dictionary&lt;K,V&gt;里V的设计决定的性能

回到目录 字典对象Dictionary<K,V>我们经常会用到,而在大数据环境下,字典使用不当可能引起性能问题,严重的可能引起内在的溢出! 字典的值建议为简单类型,反正使用Tuple<T> 字典的键在查找时,时间复杂度为O(1),性能不会有任何问题,所以不要愿望它 下面代码是对500万的字典进行测试,首先赋值,然后取出一个随机机,性能在毫秒级 static void Draw() { int count = 5000000; Console.WriteLine("test

ArrayList,Hashtable,List&lt;T&gt;,Dictionary&lt;K,V&gt;

1.ArrayList ArrayList list = new ArrayList(); //for遍历 for (int i = 0; i < list.Count; i++) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } //foreach遍历 foreach(Object obj in list) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } 2.Hashtable Hasht

字典集合Dictionary&lt;K,V&gt;和构造的应用==&gt;&gt;体检套餐项目

效果 首先,我们先来准备我们需要的类 1.检查项目类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 第五章_体检套餐管理系统_ { //项目类 public class HealthCheckItem { //项目描述 public string Description { get; set;

c# Dictionary&lt;K,V&gt;

原文地址:https://www.cnblogs.com/fanweisheng/p/11517709.html

Java集合源码分析(七)HashMap&lt;K, V&gt;

一.HashMap概述 HashMap基于哈希表的 Map 接口的实现.此实现提供所有可选的映射操作,并允许使用 null 值和 null 键.(除了不同步和允许使用 null 之外,HashMap 类与 Hashtable 大致相同.)此类不保证映射的顺序,特别是它不保证该顺序恒久不变. 值得注意的是HashMap不是线程安全的,如果想要线程安全的HashMap,可以通过Collections类的静态方法synchronizedMap获得线程安全的HashMap. Map map = Coll