二維數(shù)組 交錯(cuò)數(shù)組 foreach遍歷

】二維數(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ù)組

類型 [ ] [ ] 數(shù)組名 = new 類型 [數(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);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,771評(píng)論 0 33
  • 知識(shí)點(diǎn): 注:int類型默認(rèn)32位有大小范圍 且第一位為符號(hào)位 0 為正 1 為負(fù) 8.4作業(yè) A:1、風(fēng)力預(yù)警系...
    cGunsNRoses閱讀 1,121評(píng)論 0 0
  • 8、15work 1、自己獨(dú)立寫出冒泡排序及選擇排序 一、冒泡排序 二、選擇排序 2. 找出數(shù)組元素中,差值(絕對(duì)...
    cGunsNRoses閱讀 365評(píng)論 0 0
  • 定義方法1int[,]twodim=newint[3,3];twodim[0,0]=1;twodim[0,1]=2...
    Unity開發(fā)閱讀 725評(píng)論 0 0
  • 8yue16 二維數(shù)組 一維是一條線 二維則是一個(gè)面(表) 有兩個(gè)下標(biāo)的數(shù)組,本質(zhì)上是以數(shù)組作為數(shù)組元素的數(shù)組 類...
    cGunsNRoses閱讀 757評(píng)論 0 0