首页 优化推广 C#集合

C#集合

来源: | 时间:2013/1/5 22:16:54 |
System.Collections 命名空间包含接口和类,这些接口和类定义各种对象(如列表、队列、位数组、哈希表和字典)的集合。
System.Collections.Generic 命名空间包含定义泛型集合的接口和类,泛型集合允许用户创建强类型集合,它能提供比非泛型强类型集合更好的类型安全性和性能。
System.Collections.Specialized 命名空间包含专用的和强类型的集合,例如,链接的列表词典、位向量以及只包含字符串的集合。

在System.Collections命名空间中提供了许多接口:

  • IEnumerable循环集合项目
  • ICollection可以获取集合中项目个数
  • IList项目列表
  • IDictionary提供了键码索引
 
 

(一)ArrayList 类:使用大小可按需动态增加的数组。


 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ArrayList al = new ArrayList();
            al.Add(100);//单个添加
            foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
            {
                al.Add(number);//集体添加方法一
            }
            int[] number2 = new int[2] { 11, 12 };
            al.AddRange(number2);//集体添加方法二
            al.Remove(3);//移除值为3的
            al.RemoveAt(3);//移除第3个
            ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份


            Console.WriteLine("遍历方法一:");
            foreach (int i in al)//不要强制转换
            {
                Console.WriteLine(i);//遍历方法一
            }

            Console.WriteLine("遍历方法二:");
            for (int i = 0; i < al2.Count; i++)//数组是length
            {
                int number = (int)al2[i];//一定要强制转换
                Console.WriteLine(number);//遍历方法二

            }
        }
    }
}
 
 
 

(二)Queue:队列,表示对象的先进先出集合。Enqueue方法入队列,Dequeue方法出队列。


 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
  <

服务热线

153 8323 9821

功能和特性

价格和优惠

网站和维护

推广和优化

微信服务号