】二維數(shù)組 ? ? ? ? ? ? ? ? ? ? ?衡行縱列
int[,] arr = new int[2,3]{{2,3,4},{5,6,7}};? ? ? ? ? ? //2行? 3列? ? 動(dòng)態(tài)初始化
int[,] arr_1 = new int[,]{{1,3},{4,5},{6,7}};? ? ? ? ? //3行? 2列
int[,] arr_2 = {{12,34,45},{32,16,25} }; ? ? ? ? ? ? //2行 ?3列 ? ? 靜態(tài)初始化? ? ? ? ? ? ? ? ?
for (int i = 0; i < 2; i++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//行數(shù)
? ? ? ? ? ? for (int j = 0; j < 3; j++) { ? ? ? ? ? ? ? ? ? ? ? ? //列數(shù)
? ? ? ? ? ? ? ? ? ? ? ?Console.Write("{0} ",arr[i,j]);
? ? ? ? ? ? ? }
Console.WriteLine (); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //換行
}
二維數(shù)組中酒繁,第n個(gè)元素對(duì)應(yīng)的值為
int [a,b];
i = (n-1)/b;
j = (n-1)%b;
兩行三列? =>? 三行兩列
int[,] arr = new int[2,3]{{2,3,4},{5,6,7}};
int[,] arr_1 = new int[3,2];
for (int i = 0; i < 2; i++) {
? ? ? ? ? ? for (int j = 0; j < 3; j++) {
? ? ? ? ? ? ? ? ? ? ? ?arr_1[j,i] = arr[i,j];? ? ? ? ? ? ? ? ? ? //行列交換
? ? ? ? ? ? ?}
}
for (int a = 0; a < 3; a++) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? //遍歷數(shù)組 打印
? ? ? ? ? ? for (int b = 0; b < 2; b++) {
? ? ? ? ? ? ? ? ? ? ? ? Console.Write("{0} ",arr_1[a,b]);
? ? ? ? ? ? ? }
Console.WriteLine ();
}
//求最大元素及所在行和列
int[,] arr_1 = new int[,]{{1,3,5,6},{5,6,4,5},{6,7,5,8}};
//記錄最大值及所在的下標(biāo)
int max = arr_1 [0, 0], x=0, y=0;
for (int i = 0; i < 3; i++) {
? ? ? ? ? ? for (int j = 0; j < 4; j++) {
? ? ? ? ? ? ? ? ? ? ? ? if (max <arr_1[i,j]){
? ? ? ? ? ? ? ? ? ? ? ? ? ? max = arr_[i,j];
? ? ? ? ? ? ? ? ? ? ? ? ? ? x=i; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//確定下標(biāo)
? ? ? ? ? ? ? ? ? ? ? ? ? ? y=j;
? ? ? ? ? ? ? }
? ? ? ?}
}
Console.WriteLine ("max= "+max+"x= "+x+"y= "+y);
對(duì)角線元素和
int sum = 0;
int[,] arr = new int[3, 3]{{1,2,3},{4,5,6},{7,8,9} };
for (int i = 0; i < 3; i++) {
? ? ? ? ? ? for (int j = 0; j < 3; j++) {
? ? ? ? ? ? ? ? ? ? ? ?if (i == j) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? sum += arr[i,j];
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ?}
}
Console.WriteLine ("{0}",sum);
交錯(cuò)數(shù)組
類(lèi)型 [ ] [ ] 數(shù)組名 = new 類(lèi)型 [數(shù)組長(zhǎng)度][ ];
例如? ? int [ ] [ ] arr? = new int[3][ ];
int[ ] arr_1 = {1,3,5 };
int[ ] arr_2 = {2,4 };
int[ ] arr_3 = {23,45,12,34 };
int [ ][ ] arr = new int[3][ ]{arr_1,arr_2,arr_3};
arr [0] = new int[ ]{27,23,56 }; ? ? ? ? ? ? ? ? ? ? ? ? ? ? //修改下標(biāo)為0的元素
arr [1] = arr_3; ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//使下標(biāo)為1的元素等于arr_3的元素
//遍歷數(shù)組
for (int i = 0; i < arr.Length; i++) {
? ? ? ? ? ? for (int j = 0; j < arr[i].Length; j++) {
? ? ? ? ? ? ? ? ? ? ? ?Console.Write ("{0} ",arr[i][j]);
? ? ? ? ? ? ? }
Console.WriteLine();
}
foreach遍歷數(shù)組
int[ ] arr = {1,2,5,6,8,7};
int sum = 1;
foreach (var a in arr) { ? ? ? ? ? ? ? ? ? ? ? //a不能進(jìn)行修改
? ? ? ? ? ? ? ?sum *= a;
}
Console.WriteLine ("{0}",sum);
string str = "Hello,lanou!";
char[ ] chars = str.ToCharArray(); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //得到字符串的字符數(shù)組
//char[ ] chars_1 = "Hello,lanou!".ToCharArray();
int sum = 0;
for (int i = 0; i < chars.Length; i++) {
? ? ? ? ? ? if (chars[i] =='l') {
? ? ? ? ? ? ? ? ?++sum
? ? ? ? ? }
}
Console.WriteLine ("{0}",sum);
string str = "Hello,lanou!";
char[ ] chars = str.ToCharArray();
// char[ ] chars_1 = "Hello,lanou!".ToCharArray();
int sum = 0;
foreach (var ch in chars) {
? ? ? ? ? ? ? ?if (ch == 'l') {
? ? ? ? ? ? ? ? ? ? ++sum;
}
Console.WriteLine ("{0}",sum);