C#——数组

1.初始化

与C++区别:
C#:   int[] Array;   √ 可以先声明成未知大小,后面再new大小和赋值
C++:  int Array[];   × error C2133: “Array”: 未知的大小

C#:   int[] Array={1,2,3,4,5};    √ 
C++:  int Array[5]{1,2,3,4,5};     √ 

C#:   int[] myArray=new int[5]{ 1,2,3};   × error CS0847: 应为一个长度为“5”的数组初始值设定项
C++:  int *Array = new int[5]{ 1,2,3 };   √  自动补0

C#:   int[] Array = new int[5];   √  自动补0
C++:  int *Array = new int[5];    ×  野指针        

2.二维数组

C#:
int[,] arr = new int[2,3];

int[][] arr = new int[2][];   //不能指定列数
arr[0] = new int[2];          //可以是不规则数组
arr[1] = new int[]{4,5,6};         

C++:
int (*arr)[3] = new int[2][3];
int **arr = new int*[2];

3.动态数组  ArryList类

using System.Collections;

int[] arr = { 13, 85, 35, 67, 5 };
ArrayList arrList = new ArrayList(arr);     
arrList.Add(22);
Console.WriteLine("arrList's capacity is {0}", arrList.Capacity);     //容量
Console.WriteLine("arrList has {0} numbers", arrList.Count);          //元素个数

 if (arrList.Contains(67))    //判断某个元素是否在数组中
{
    Console.WriteLine(arrList.IndexOf(67));    //返回某个元素第一次出现的索引
}
arrList.Insert(5, 99);     //在指定索引处插入一个元素
arrList.Remove(67);        //移除第一次出现的指定对象
arrList.RemoveAt(2);       //移除指定索引处的元素
arrList.RemoveRange(2, 2); //移除指定范围的元素
arrList.Sort();
foreach (int i in arrList)
{
    Console.WriteLine(i);
}
ArrayList al = new ArrayList() { 88, 67, 44 };
arrList.Insert(0, al);      //在指定索引处插入一个元素集合,变成不规则二维数组,arrList[0]有3列
arrList.Reverse();          //反转数组,注意反转≠逆序 
arrList.TrimToSize();       //设置容量为实际元素个数
arrList.Clear();            //清除所有元素

4.哈希表 Hashtable 

using System.Collections;
Hashtable hashtable = new Hashtable();
hashtable.Add("小明", 98);
hashtable.Add("小花", 100);
hashtable.Add("红红", 60);
if (hashtable.ContainsKey("红红") || hashtable.ContainsValue(60)) 
{
    hashtable.Remove("红红");
}
foreach(DictionaryEntry de in hashtable)
{
    Console.Write(de.Key + " ");
    Console.Write(de.Value);
}
hashtable.Clear();
原文地址:https://www.cnblogs.com/tomatokely/p/14905415.html