数组: 必须规定类型,规定长度
1.定义
int[] i = new int[5];
int[] j = new int[] { 1, 2, 3, 4, 5 };
string[] k = new string[] { "a", "b", "c", "d", "e" };
2.数组的遍历
Console.WriteLine(j[0]);
for (int x = 0; x < j.Length; x++)
{
Console.WriteLine(j[x]);
}
foreach (int temp in j)
{
Console.WriteLine(temp);
}
foreach (var temp in k)
{
Console.WriteLine(temp);
}
3.数组赋值
j[0] = 4;
int[] x = new int[5];
var x = j.Clone();
4.二维数组
int[,] erw = new int[2, 2];
int[,] erw2 = new int[,] { { 4, 5 }, { 7, 8 } };
Console.WriteLine(erw2[1, 0]);//[1][0]
集合:不需要规定类型 / 长度
1.定义
ArrayList arr = new ArrayList();
DateTime dt = new DateTime(2017, 11, 3);
arr.Add("abc");
arr.Add(123);
arr.Add(true);
arr.Add(dt);
2.集合遍历
foreach (var x in arr)
{
Console.WriteLine(x);
}
arr.Remove(123);
arr.RemoveAt(1);
arr.Reverse();//顺序翻转
arr.Insert(2, "啦啦啦");
Console.WriteLine(arr.Contains(123));
foreach (var x in arr)
{
Console.WriteLine(x);
}
泛型集合:规定类型,不规定长度
1.定义
List<string> a = new List<string>();
2.赋值
a.add(“11111”);
3.插入
a.insert(0."2222");