2018-10-11陣列
陣列是一個有固定大小的儲存相同型別元素的集合
一次性儲存多個相同型別的變數
陣列的申明:
資料型別[] 資料名=new 資料型別[陣列長度];
宣告沒有資料的陣列(申明陣列必須有長度);
int[] array=new int[0];
var array=new int[0];
string array=new string[0];
int[] intArray = new int[4];
string[] stringArray = new string[5];
宣告有資料的陣列;
int[] intArray1 = new int[4] {1, 2, 3, 4};
int[] intArray2 = new int[] {1, 2, 3, 4, 5};
int[] intArray1 = new int[4] {1, 2, 3, 4};
int[] intArray2 = new int[] {1, 2, 3, 4, 5};
資料的賦值:
stringArray[0] = "1";
intArray[0] = 1;
intArray[1] = 2;
stringArray[0] = "1"; intArray[0] = 1; intArray[1] = 2;
陣列的取值:
直接通過資料的索引取值
Console.WriteLine(intArray[1]);
Console.WriteLine(intArray[1]);
二維陣列:
int[,] intArr = new int[4,2];
intArr[0, 0] = 10;
intArr[1, 0] = 15;
intArr[2, 0] = 20;
int[,] intArr = new int[4,2];
intArr[0, 0] = 10;
intArr[1, 0] = 15;
intArr[2, 0] = 20;
int[,] intArr1 = new int[3, 3] {{01, 02,123}, {011, 111,456},{456,987,852}};
//GetLength()獲取軸向下標
for (int i = 0; i < intArr1.GetLength(0); i++)
{
for (int j = 0; j < intArr1.GetLength(1); j++)
{
Console.Write(intArr1[i,j]+"\t");
}
Console.WriteLine();
}
int[,] intArr1 = new int[3, 3] {{01, 02,123}, {011, 111,456},{456,987,852}};
//GetLength()獲取軸向下標
for (int i = 0; i < intArr1.GetLength(0); i++)
{
for (int j = 0; j < intArr1.GetLength(1); j++)
{
Console.Write(intArr1[i,j]+"\t");
}
Console.WriteLine();
}