Array:
需要处理的元素数量确定并且需要使用下标时可以考虑,不过建议使用List
数组的内容都是相同类型
数组可以直接通过下标访问
创建时需要固定数组大小
int size = 5;
int [] test = new int [size];
string [] test2 = new string [3];
//赋值
test2[0] = "chen" ;
test2[1] = "j" ;
test2[2] = "d" ;
//修改
test2[0] = "chenjd" ;
//排序
int[] myArray = {
4, 3, 5, 1, 2 };
Array.Sort(myArray);
//获取索引
int value = Array.IndexOf(myArray, 5);
//Array.Copy(sourceArray, destinationArray, length): 将一个数组的元素复制到另一个数组中。
int[] myArray = {
4, 3, 5, 1, 2 };
int[] newArray = new int[6];
//Array.Copy(sourceArray, destinationArray, length)
//length是从原数组复制的长度
Array.Copy(myArray, newArray, 4);
ArrayList:
不必在声明ArrayList时指定它的长度
ArrayList可以存储不同类型的元素
ArrayList不是类型安全的
引入时,泛型还未出现,后泛型集合类(如 List)因其类型安全性和性能优势而变得流行
常用方法:
//new
ArrayList arrayList = new ArrayList();
//Add
arrayList.Add("Hello");
arrayList.Add(