一、簡介
一次存儲多個相同類型的變量
二、語法
數組類型[] 數組名=new 數組類型[數組長度];
int[] nums = new int[10]; 或者 int[] numsTwo = { 1, 2, 3, 4, 5, 6 };
如果你想要訪問到數組中某一塊元素,需要通過這個元素的下標或者索引去訪問
注意點:數組長度一旦固定了,不能改變了。
三、實例
我們通過一個循環給數組賦值,同樣,也通過一個循環對數組進行取值;
//我們通過一個循環給數組賦值,同樣,也通過一個循環對數組進行取值 int[] nums = new int[10]; for (int i = 0; i < nums.Length; i++) { nums[i] = i; } for (int i = 0; i < nums.Length; i++) { Console.WriteLine(nums[i]); } Console.ReadKey();
輸出結果