0%

C# —— 陣列、陣列方法

陣列將同性質資料集中放在連續記憶體位址

例如我們如果想要宣告5個整數,不是用陣列的話要一個一個宣告像是

int a,b,c,d,e; 但如果用陣列宣告就可以像這樣 int []scores = new int[5];

宣告陣列的方法

1
2
3
資料型別 [] 陣列名稱 = new 資料型別[陣列大小];
ex: int []scores = new int[5];

陣列的初值設定

1
2
3
4
5
int []scores = new int[5]; //宣告陣列
scores[0] = 90;scores[1] = 85;scores[2] = 92;scores[3] =75;
scores[4] = 80; //這樣一個一個賦予值是不是很麻煩呢~
//所以也可以像這樣
int[]scores = new int[]=90,85,92,75,80};//不指定大小,直接賦予相對應的值

使用迴圈存取陣列內容

1
2
3
4
5
6
7
8
9
//例如有5個成績,想要算這5個成績的平均
int[]scores = new int[]{90,85,92,75,80};
int sum,avg ;
sum = 0 ;
for(int i=0 ;i<scores.Length; i++)
{
sum += score[i];//累加成績
}
avg = sum / scores.Length ;

若for的條件運算式(終值)無法確定時,可改用foreach敘述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
foreach(資料型別 element in group)
{
敘述區塊
[break;]
敘述區塊
}
element:是一個變數名稱,可將陣列中每個元素依序指定給element
group:是一個物件變數,必須是物件集合或陣列名稱
break:若要提前離開迴圈,可在欲離開處使用brea
ex:將scores陣列中所有成績相加置入sum變數中
int[]scores = new int[]={90,85,92,75,80};
int sum = 0;
foreach(int point in scores) //point用來存放scores裡每個元素
{
sum+=point ;
}

陣列的排序

Array.Sort()方法可以將指定陣列內的元素由小到大遞增排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//寫法
//Array.Sort(陣列名稱1,陣列名稱2);//最多兩個引數
//ex1:成績排序
int[] scores = new scores[]{94,85,89};
Array.Sort(scores);
排序前: scores[0]=94 scores[1]=85 scores[2]=89
排序後: scores[0]=85 scores[1]=89 scores[2]=94
//ex2:成績表含有數學和學生姓名,需要兩個陣列來存放,如果只針對數學成績排序會導致姓名和成績不一致,因此Array.Sort()方法允許使用兩個引數
int[] scores = new scores[]{94,85,89};
string[] sName = new sName[]{"小明","大王","阿花"};
Array.Sort(scores,sName);
//排序前:
scores[0]=94 scores[1]=85 scores[2]=89
sName[0]="小明"sName[1]="大王"sName[2]="阿花"
//排序後:
scores[0]=85 scores[1]=89 scores[2]=94
sName[0]="大王"sName[1]="阿花"sName[2]="小明"

陣列的反轉

Array.Sort()可以將陣列由小到大遞增,那如果今天想要由大到小遞減就需要用到**Array.Reverse(陣列名稱)**,但Array.Reverse只提供一個引數,若兩個陣列有相關聯,反轉時兩個陣列都要做Array.Reverse

1
2
3
4
5
6
7
8
9
10
11
12
//例如把上面ex2反轉
int[] scores = new scores[]{94,85,89};
string[] sName = new sName[]{"小明","大王","阿花"};
Array.Sort(scores,sName);
Array.Reverse(scores);//反轉成績
Array.Reverse(sName);//反轉姓名
//反轉前 :
scores[0]=85 scores[1]=89 scores[2]=94
sName[0]="大王"sName[1]="阿花"sName[2]="小明"
//反轉後 :
scores[0]=94 scores[1]=89 scores[2]=85
sName[0]="小明"sName[1]="阿花"sName[2]="大王"

陣列的搜尋

Array.IndexOf()方法,可以從指定的一維陣列中找尋指定資料是否存在,若找到相符合的值就回傳該陣列所在的索引位置,若沒找到就回傳-1

1
2
3
4
5
6
7
8
//寫法
// 變數名稱 = Array.IndexOf(陣列名稱,欲想找得值,count);
//count:由指定的索引位置往後找幾個元素,若count省略表示搜尋整個陣列
//ex1
int[] scores = new scores[]{94,85,89,90,85};
int num1 =Array.IndexOf(scores,94);//回傳0
int num2 =Array.IndexOf(scores,85,3);//回傳4,因為從第3個元素開始找
int num3 =Array.IndexOf(scores,70);//回傳-1,找不到

陣列常用的屬性與方法

Rank屬性 :如果想知道某陣列是幾維陣列就可以使用

1
2
3
4
5
6
int[,,]scores1= new int[2,16,24];
int[ ,]scores2 = new int[,]{{90,85,77},{81,68,98}};
int[] scores3 = new int[]{87,69,89};
int value1 =scores1.Rank;//3,因為是三維陣列
int value2 =scores2.Rank;//2,因為是二維陣列
int value3 =scores3.Rank;//1,因為是一維陣列

Length屬性:可以取得陣列的長度,即為陣列元素的總數

1
2
int[] scores3 = new int[]{87,69,89};
int value3 = scores3.Length; //3

GetLength方法:可以取得陣列中”指定維度”的長度,此方法只有一個參數,就是陣列的維度(第1個維度為0,第2個維度為1….)

1
2
3
4
5
int[ ,]scores2 = new int[,]{{90,85,77,88},{81,68,98,86}};
int[] scores3 = new int[]{87,69,89};
int value3 = scores3.GetLength(0);//3
int value2 = scores2.GetLength(0);//2
int value1 = scores2.GetLength(1);//4

CopyTo方法:可以將一個陣列元素複製到另一個目標陣列中,而且可以指定從第幾個索引值開始複製

1
2
3
int[] scores1 = new int[]{87,69,89};
int[] scores2 = new int[3];
scores1.CopyTo(scores2,0);//將scores1的元素第0個索引位置開始複製scores2

Split方法:屬於字串物件的方法,可以根據間隔字元將字串分割成陣列元素值

1
2
3
string scores = "95,88,76,69,92";
string []aScores = scores.split(',');//指定以,字元來分割字串
執行後: aScores[0]=95 aScores[1]=88 aScores[2]=76 aScores[3]=69 aScores[4]=92

在C#中陣列實際上是一個物件,由於陣列是屬型別參照的一種,必須使用new來建立一個陣列物件,所以陣列無法像變數一樣宣告後即可使用,必須先經宣告後,用new來建立陣列的實體

參考:
Visual C# 2017基礎必修課

另外有寫C# —集合(ArrayList、List、Dictionary),可以參考看看哦。

文章若有錯誤或想交流,還不吝嗇給予指教哦