集合与泛型>集合

集合可以分为泛型集合类和非泛型集合类。

泛型集合类一般位于System.Collections.Generic命名空间,非泛型集合类位于System.Collections命名空间,除此之外,System.Collection.Specialized命名空间也有些集合类。

数组集合类     System.Collections.ArrayList;
布尔集合类     System.Collections.BitArray;
队列            System.Collections.Queue;
堆栈            System.Collections.Stack;
哈希表            System.Collections.Hashtable;
排序集合类            System.Collections.SortedList;

 

集合与泛型>数组集合>创建列表

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ArrayListDemo1
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
//使用默认的初始容量创建ArrayList,该实例并没有任何元素。
            ArrayList al1 = new ArrayList();
            al1.Add(
"111");
            al1.Add(
"222");
            al1.Add(
"333");
            DisplayResult(al1);
            
//使用实现了ICollection接口的集合类来初始化新创建的ArrayList,该实例与参数中的集合具有相同的初始容量。
            ArrayList al2 = new ArrayList(al1);
            DisplayResult(al2);
            
//经由指定一个整数值来初始化ArrayList的容量。
            ArrayList al3 = new ArrayList(20);
            DisplayResult(al3);
            ArrayList al4
=ArrayList.Repeat("ccc"4);
            DisplayResult(al4);
            Console.ReadLine();
          }
        
static void DisplayResult(ArrayList ls)
        {
            Console.WriteLine(
"");
            
if (ls.Count <= 0)
            {
                Console.WriteLine(
"数合没有任何集合元素");
            }
            
foreach (object s in ls)
            {
                Console.Write(Convert.ToString(s));
            }
        }
    }
}

相关文章: