1数组声明
变量类型[ ] 数组名 声明了一个数组,但未赋值,内存没空间
cs
int[] arry1;
变量类型[ ] 数组名 = new 变量类型[数组的长度] 有空间,默认都为0
cs
int[] arr2 = new int[5];
变量类型[ ]数组名= new 变量类型[数组长度]{内容1,内容2} 有空间,自定义赋值
cs
int[] arry3 = new int[5] { 1, 2, 3, 4, 5 };
变量类型[]数组名= new 变量类型[ ]{内容1,内容2} 有空间,自定义赋值 数组长度为赋值个数
cs
int[] arry4 = new int []{ 1, 2 };
变量类型[]数组名={内容1,内容2}
cs
int[] arry5 = { 1, 2, 3, 4, 5 };
2数组使用
cs
int[] array = new int[5] { 1, 2, 3, 4,5 };
数组中的下标和索引从0开始的,访问不越界
cs
for(int i = 0; i < array.Length; i++)
{
Console.WriteLine(array[i]);
}
增加元素,不能在原有数组上直接增加新元素,将数组赋值给另一个数组
cs
int[] array2 = new int[6];
for(int i = 0; i < array.Length; i++)
{
array2[i] = array[i];
}
array = array2;