如何在 C Sharp 中定義一維陣列?
陣列用於儲存資料集合,但通常將陣列視為儲存在連續記憶體位置的同類型變數的集合更有用。
定義一維陣列 −
int[] runs = new int[10];
我們現在在同一行中初始化陣列 −
int[] runs = new int[5] {125, 173, 190, 264, 188};以下是一個顯示如何宣告、初始化和顯示陣列的示例 −
示例
using System;
namespace Program {
class Demo {
static void Main(string[] args) {
int[] runs = new int[5] {125, 173, 190, 264, 188};
int i,j;
for (j = 0; j < 5; j++ ) {
Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]);
}
Console.ReadKey();
}
}
}輸出
Innings score of Cricketer[0] = 125 Innings score of Cricketer[1] = 173 Innings score of Cricketer[2] = 190 Innings score of Cricketer[3] = 264 Innings score of Cricketer[4] = 188
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
html
CSS
安卓
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP