二維數組:
定義二維數組 int[,] myArray = new int[幾個一維數組,數組中的個數];
?數組可以具有多個維度。例如,下列聲明創建一個四行兩列的二維數組(可以理解為4個1維數組,數組中包含2個元素):
int[,] myArray = new int[4,2];
int[,] myArray = new ?int[4,2] {{1,2}, {3,4}, {5,6}, {7,8}};
取值則是
Int i=myArray[0,0]
輸出結果i為:1
Int i=myArray[0,1]
輸出結果i為:2
?
多維數組:
數組可以具有多個維度。例如:
int[,,] myArray = new ?int[2,4,2] {{1,2},{3,4},{5,6},{7,8}} , {{9,10},{11,12},{13,14},{15,16}};
一個元素是一個點?一維數組是一條線?二維數組一個面(表格)?三維數組是一個教學樓(立體)
split() ?
以***進行分割
分割開的內容需要放置在string類型的數組中,不需要給數組定義長度
Console.Write("請輸入姓名-年齡-工作單位:");//"張三-33-漢企" string s = Console.ReadLine() ; string[] array = s.Split('-'); foreach(string aa in array) {Console.WriteLine(aa);}
上面是一位數組的一個題,前面做過,這次用的是一個數組!!
輸入班級人數,輸入每個人的語數英成績求語文兩個最高分,數學兩個最低分,英語平均分


Console.Write("請輸入班級人數:");int a = int.Parse(Console.ReadLine());double[,] s = new double[a, 3];for (int i = 0; i < a; i++){Console.Write("請輸入第{0}個人的語文成績:", (i + 1));s[i, 0] = double.Parse(Console.ReadLine());Console.Write("請輸入第{0}個人的數學成績:", (i + 1));s[i, 1] = double.Parse(Console.ReadLine());Console.Write("請輸入第{0}個人的英語成績:", (i + 1));s[i, 2] = double.Parse(Console.ReadLine());}for (int i = 0; i < a; i++){for (int j = 0; j < 3; j++){Console.Write(s[i, j] + "\t");}Console.WriteLine();}for (int i = 0; i < a - 1; i++){for (int j = i + 1; j < a; j++){if (s[i, 0] < s[j, 0]){double z = s[i, 0];s[i, 0] = s[j, 0];s[j, 0] = z;double z1 = s[i, 1];s[i, 1] = s[j, 1];s[j, 1] = z1;double z2 = s[i, 2];s[i, 2] = s[j, 2];s[j, 2] = z1;}}}Console.WriteLine("語文最高分為:" + s[0, 0] + "," + s[1, 0]);for (int i = 0; i < a - 1; i++){for (int j = i + 1; j < a; j++){if (s[i, 1] > s[j, 1]){double z = s[i, 0];s[i, 0] = s[j, 0];s[j, 0] = z;double z1 = s[i, 1];s[i, 1] = s[j, 1];s[j, 1] = z1;double z2 = s[i, 2];s[i, 2] = s[j, 2];s[j, 2] = z1;}}}Console.WriteLine("數學最低分為:" + s[0, 1] + "," + s[1, 1]);double sum = 0;for (int i = 0; i < a; i++){sum += s[i, 2];}Console.WriteLine("英語平均分為:" + sum / a);Console.ReadLine();
注意冒泡循環中,隨著想要得到的分數互換的同時,也要把同一數組的其他項也互換,不然會影響其他結果。
?查找替換問題


Console.Write("春眠不覺曉,處處聞啼鳥。夜來風雨聲,花落知多少。\n");Console.Write("請輸入要替換的文字:");string a = Console.ReadLine();Console.Write("請輸入要替換為的文字:");string b = Console.ReadLine();string[] s = new string[24] { "春", "眠", "不", "覺", "曉", ",", "處", "處", "聞", "啼", "鳥", "。", "夜", "來", "風", "雨", "聲", ",", "花", "落", "知", "多", "少", "。" };for (int i = 0; i < 24; i++){if (s[i] == a){s[i] = b;}}Console.Write("替換成功,替換后為:");foreach (string aa in s){Console.Write(aa);}Console.ReadLine();
?


string a = "春眠不覺曉,處處聞啼鳥。夜來風雨聲,花落知多少。";Console.Write("請輸入要替換的字:");string b = Console.ReadLine();Console.Write("請輸入替換為的文字:");string c = Console.ReadLine();Console.WriteLine(a.Replace(b, c));Console.ReadLine();
?
?
?
?
?