Chinaunix首页 | 论坛 | 博客
  • 博客访问: 443561
  • 博文数量: 155
  • 博客积分: 786
  • 博客等级: 军士长
  • 技术积分: 1561
  • 用 户 组: 普通用户
  • 注册时间: 2012-09-01 23:37
个人简介

在路上

文章分类

全部博文(155)

文章存档

2016年(2)

2015年(36)

2014年(45)

2013年(34)

2012年(38)

我的朋友

分类: C#/.net

2015-08-21 00:11:00

List 泛型集合

点击(此处)折叠或打开

  1.           //创建泛型集合对象
  2.             List<int> list = new List<int>();
  3.             //list.Add(1);
  4.             //list.Add(2);
  5.             //list.Add(3);

  6.             list.AddRange(new int[] { 1, 2, 3, 4, 5, 6 });

  7.             list.AddRange(new int[] { 1, 2, 3 });

  8.             list.AddRange(list);

  9.             //List泛型集合可以转换为数组
  10.             int[] nums = list.ToArray();

  11.             List<string> listStr = new List<string>();

  12.             string[] str = listStr.ToArray();


  13.             char[] chs = new char[] { 'c', 'b', 'a' };
  14.             List<char> listChar = chs.ToList();
  15.             for (int i = 0; i < listChar.Count; i++)
  16.             {
  17.                 Console.WriteLine(listChar[i]);
  18.             }

  19.             List<int> listTwo = nums.ToList();


  20.             for (int i = 0; i < list.Count; i++)
  21.             {
  22.                 Console.WriteLine(list[i]);
  23.             }
  24.             Console.ReadKey();
  25.         }
字典集合

点击(此处)折叠或打开

  1.             Dictionary<int, string> dic = new Dictionary<int, string>();
  2.             dic.Add(1, "张三");
  3.             dic.Add(2, "李四");
  4.             dic.Add(3, "王五");
  5.             dic[1] = "新来的";
  6.             foreach (KeyValuePair<int,string> kv in dic)
  7.             {
  8.                 Console.WriteLine("{0}---{1}",kv.Key,kv.Value);
  9.             }

  10.             foreach (var item in dic.Keys)
  11.             {
  12.                 Console.WriteLine("{0}---{1}", item, dic[item]);
  13.             }
  14.             Console.ReadKey();


阅读(443) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~