C#集合的一些例子

List<T>

using System.Collection.Generic;


public class Program {
       public static void Main() {
              List<int> list = new List<int>(3);
              for (int i=0; i<8; i++) {
               <span style="white-space:pre">	</span> list.Add(i);
<span style="white-space:pre">	</span>       <span style="white-space:pre">	</span> System.Console.WriteLine(“Count: {0} Capacity: {1}”,
<span style="white-space:pre">		</span> list.Count, list.Capacity);
             <span style="white-space:pre">	</span> }
<span style="white-space:pre">	</span>      <span style="white-space:pre">	</span>list.TrimExcess();
              <span style="white-space:pre">	</span>System.Console.WriteLine(“Count: {0}  Capacity: {1}”,
            <span style="white-space:pre">	</span>list.Count, list.Capacity );
        }
}

HashTable

using System.Collections;
public class Program {

       public static void Main() {

           //Create a hash table
           Hashtable ht = new Hashtable();
           //add elements to the table
           ht.Add("house", "dwelling");
           // ht.Add("house", "houseing");  //HashTable的键值是唯一的,否则会有异常
           ht.Add("car", "means of transport");
           ht.Add("apple", "Ediable fruit");
           ht["tractor"] = "Farm implements";
           //首先,使用Keys属性取回键的集合。接着,使用每个键索引ht,得到每个键相关的内容
           ICollection c=ht.Keys;
           foreach (string str in c)
               System.Console.WriteLine(str+":"+ht[str]);
        }

}

SortedList

SortedList类创建了一个存储"键/值"对的集合,该集合中的元素参照键的值顺序地存储。

通过枚举器访问集合


using System.Collections;
using System;
using System.Collections.Generic;
public class Program {

       public static void Main() {

           //Create a hash table
           Hashtable ht = new Hashtable();
           //add elements to the table
           ht.Add("house", "dwelling");
           // ht.Add("house", "houseing");  //HashTable的键值是唯一的,否则会有异常
           ht.Add("car", "means of transport");
           ht.Add("apple", "Ediable fruit");
           ht["tractor"] = "Farm implements";
           //首先,使用Keys属性取回键的集合。接着,使用每个键索引ht,得到每个键相关的内容
           ICollection c=ht.Keys;
           foreach (string str in c)
               System.Console.WriteLine(str+":"+ht[str]);
           IDictionaryEnumerator ide = ht.GetEnumerator();
           while (ide.MoveNext()) {
               Console.Write(ide.Entry.Key+":"+ide.Entry.Value);
               Console.Write(ide.Key + ":" + ide.Value);
               Console.WriteLine();
           }

           List<int> list = new List<int>(5);
           list.Add(2);
           list.Add(4);
           list.Add(7);
           list.Add(9);
           IEnumerator idic = list.GetEnumerator();
           while(idic.MoveNext()){
               Console.WriteLine(idic.Current);
           }
        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值