二维数组,锯齿数组和集合 C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用 C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)...
一.二維數組
二維數組:
一維數組----豆角
二維數組----表格
定義:
1.一維數組:
數據類型[] 數組變量名 = new 數據類型[數組長度];
數據類型[] 數組變量名 = new 數據類型[數組長度]{1,2,3....};
2.二維數組:
數據類型[,] 數組變量名 = new 數據類型[行數,列數];
int[,] a = new int[3,4];
賦值:
a[行下標,列下標] = 值 下標都是從0開始的
取值:
a[行下標,列下標]
題目:一個班6個人,從鍵盤輸入每個學號語文,數學,外語成績(不需輸入學號)。輸出:學生成績表(包括每個人的總分),每科的平均分。
附加1:試著,把不及格的用紅字顯示。
附加2:試著按照總分排序,顯示名次出來。
代碼:
1 static void Main(string[] args)2 { 3 //輸入6個學生的語文,數學,英語成績,輸出總分和名次4 int[,] a=new int[6,5];5 //輸入6 for (int i = 0; i <6; i++)7 {8 Console.Write("請輸入第{0}個學生的語文成績:",i+1);9 int yw = Convert.ToInt32(Console.ReadLine()); 10 Console.Write("請輸入第{0}個學生的數學成績:", i+1); 11 int sx = Convert.ToInt32(Console.ReadLine()); 12 Console.Write("請輸入第{0}個學生的英語成績:", i + 1); 13 int yy = Convert.ToInt32(Console.ReadLine()); 14 15 a[i, 0] = i + 1;//學號 16 a[i, 1] = yw; 17 a[i, 2] = sx; 18 a[i, 3] = yy; 19 a[i, 4] = yw + sx + yy; 20 21 } 22 23 //排序 24 for (int i = 0; i <6; i++) 25 { 26 for (int j = i; j <5; j++) 27 { 28 if (a[i,4]<a[j+1,4]) 29 { 30 int txh = a[j + 1, 0]; 31 a[j + 1, 0] = a[i, 0]; 32 a[i, 0] = txh; 33 34 int tyw = a[j + 1, 1]; 35 a[j + 1, 1] = a[i, 1]; 36 a[i, 1] = tyw; 37 38 int tsx = a[j + 1, 2]; 39 a[j + 1, 2] = a[i, 2]; 40 a[i, 2] = tsx; 41 42 int tyy = a[j + 1, 3]; 43 a[j + 1, 3] = a[i, 3]; 44 a[i, 3] = tyy; 45 46 int tzf = a[j + 1, 4]; 47 a[j + 1, 4] = a[i, 4]; 48 a[i, 4] = tzf; 49 } 50 } 51 } 52 Console.Clear(); 53 //輸出 54 Console.WriteLine("學號\t語文\t數學\t英語\t總分\t名次"); 55 for (int i = 0; i < 6; i++) 56 { 57 Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}",a[i,0],a[i,1],a[i,2],a[i,3],a[i,4],i+1); 58 } 59 int sum1 = 0, sum2 = 0, sum3 = 0; 60 for (int i = 0; i < 6; i++) 61 { 62 63 sum1 += a[i, 1]; 64 65 sum2 += a[i, 2]; 66 67 sum3 += a[i, 3]; 68 69 70 } 71 Console.WriteLine("平均分:{0}\t{1}\t{2}",sum1/6,sum2/6,sum3/6); 72 73 }二.鋸齒數據,數組的數組。
定義:
第一步:定義大數組
數據類型[][] a = new 數據類型[行數][];
第二步:定義小數組
數據類型[] a1 = new 數據類型[列數];
數據類型[] a2 = new 數據類型[列數];
......
第三步:把小數組放到大數組中
a[0] = a1;
a[1] = a2;
....
舉例:
1 static void Main(string[] args)2 { 3 //鋸齒數組4 int[][] a = new int[3][];5 int[] a1 = new int[] {3,4,5,6,7};6 int[] a2 = new int[] {1,2,3};7 int[] a3 = new int[] {7,8,9,10};8 9 a[0] = a1; 10 a[1] = a2; 11 a[2] = a3; 12 13 //顯示 14 for (int i = 0; i <a.Length; i++)//a.length=3 15 { 16 for (int j = 0; j <a[i].Length; j++) 17 { 18 Console.Write(a[i][j]+"\t"); 19 } 20 Console.Write("\n"); 21 } 22 23 }注意:
int[,] a = new int [3][4]; //錯
int[][] a = new int[3,4]; //錯
int[][] a = new int[3][4]; //錯
int[,] c = new int[3,4]; //對,這是二維數組
c.length==12
?
三.集合:
一、ArrayList 鏈表,沒有長度限制,可以隨時向時添加或刪除元素。
需要在前面加上:using System.Collections;
定義:
ArrayList a = new ArrayList();
操作:
a.Add(數據):添加
a.Insert(索引號,數據):插入
a.RemoveAt(索引號):刪除
a.Count 集合中元素的個數
取值:
a[下標]
取出來的值需要進行強制轉換。
舉例:
1 static void Main000(string[] args)2 {3 ArrayList a = new ArrayList();4 a.Add(10);5 a.Add(20);6 a.Add(25);7 8 a.Insert(1, 15);9 10 a.RemoveAt(2); 11 12 a[1] = (int)a[1] + 10; 13 14 15 for (int i = 0; i < a.Count; i++) 16 { 17 Console.WriteLine(a[i]); 18 } 19 }運行結果:
二、List<類型> 鏈表,,沒有長度限制,可以隨時向時添加或刪除元素。只能放指定類型的數據,取出來也不用強制轉換。
定義
List<類型> 變量名 = new List<類型>();
List<int> a = new List<int>();
操作:
a.Add(數據):添加
a.Insert(索引號,數據):插入
a.RemoveAt(索引號):刪除
a.Count 集合中元素的個數
a.Sort(); 排序
a.Reverse();反轉
取值
a[索引號]
舉例
1 static void Main(string[] args)2 {3 List<int> a = new List<int>();4 a.Add(5);5 a.Add(10);6 a.Add(20);7 8 a.Insert(2,15);9 a.RemoveAt(1); 10 11 a.Sort(); 12 a.Reverse(); 13 14 for (int i = 0; i < a.Count; i++) 15 { 16 Console.WriteLine(a[i]); 17 } 18 19 }運行結果:
三、Dictionary<key,value>字典或哈希表
定義
Dictionary<int,string> a = new Dictionary<int,string>();
操作:
a.Add(鍵值,數據);
a.Remove(鍵值);
a.Count;
取值:
a[鍵值]
舉例:
1 static void Main(string[] args)2 {3 Dictionary<int, string> a = new Dictionary<int, string>();4 a.Add(101,"haha");5 a.Add(103,"hehe");6 a.Add(105,"xixi");7 a.Add(107,"哈哈");8 9 a.Remove(103); 10 a[105] = "不許笑"; 11 foreach (KeyValuePair<int,string>p in a) 12 { 13 Console.WriteLine(p.Value); 14 } 15 }運行結果:
四、棧,隊列 知道就行了
棧:先進后出,不能隨機取其中任意一個值。
Stack<數據類型> a = new Stack<數據類型>();
a.Push(值);
數據類型 變量名 = a.Pop();
?
?舉例:
1 static void Main(string[] args)2 {3 Stack<int> a = new Stack<int>();4 5 //向集合里推入元素6 a.Push(10);7 a.Push(20);8 a.Push(30);9 a.Push(40); 10 //將元素一個個彈出集合,因為stack 沒有索引,所以遵循先進后出原則 11 Console.WriteLine(a.Pop()); 12 Console.WriteLine(a.Pop()); 13 Console.WriteLine(a.Pop()); 14 Console.WriteLine(a.Pop()); 15 16 }運行結果:
隊列:先進先出,不能隨機取其中任意一個值。
Queue<int> a = new Queue<int>();
a.Enqueue(值);
數據類型 變量 = a.Dequeue();
舉例:
1 static void Main(string[] args)2 {3 Queue<int> a = new Queue<int>();4 5 //進入隊列6 a.Enqueue(10);7 a.Enqueue(20);8 a.Enqueue(30);9 a.Enqueue(40); 10 11 //出隊列,先進先出 12 Console.WriteLine(a.Dequeue()); 13 Console.WriteLine(a.Dequeue()); 14 Console.WriteLine(a.Dequeue()); 15 Console.WriteLine(a.Dequeue()); 16 }運行結果:
?
2016.4.23??
?
C# 一維數組、二維數組(矩形數組)、交錯數組(鋸齒數組)的使用
2017年07月06日 15:35:42 閱讀數:856數組:是一個存儲相同類型元素且固定大小的順序集合。
數組聲明:
datatype[] arrayName;
數組類型是從抽象基類型 Array 派生的引用類型。由于此類型實現了 IEnumerable ,因此可以對 C# 中的所有數組使用 foreach 迭代,foreach循環(huán)對數組內容進行只讀訪問,所以不能改變任何元素的值。
?
? ? ??//一維數組
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int[] a = { 1,2,3,3}; //初始化
? ? ? ? ? ? int[] b = new int[] { 1,2,3,4,5};//初始化
? ? ? ? ? ? //var c= a.Intersect(b); ?//求交集
? ? ? ? ? ? var c = b.Except(a); //求差集
? ? ? ? ? ? foreach (var item in c)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(item);
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("數組A的平均值:"+ a.Average());
? ? ? ? ? ? Console.Read();
? ? ? ? }
? ??? ? //二維數組 同 多維數組 又稱矩形數組
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? int[,] myArray;
? ? ? ? ? ? myArray = new int[2, 3];//2行 3列
? ? ? ? ? ? myArray[0, 0] = 1;
? ? ? ? ? ? myArray[0, 1] = 2;
? ? ? ? ? ? myArray[0, 2] = 3;
? ? ? ? ? ? myArray[1, 0] = 4;
? ? ? ? ? ? myArray[1, 1] = 5;
? ? ? ? ? ? myArray[1, 2] = 6;
? ? ? ? ? ? foreach (var item in myArray)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(item);
? ? ? ? ? ? }
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
? ??? ? //鋸齒數組 又稱交錯數組
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? //聲明一個鋸齒數組 ?三行
? ? ? ? ? ? int[][] myArray;
? ? ? ? ? ? myArray = new int[3][];
? ? ? ? ? ? myArray[0] = new int[] { 1,2,3,4,5};
? ? ? ? ? ? myArray[1] = new int[3] { 1,4,6};
? ? ? ? ? ? myArray[2] = new int[] { 2,3,4,5};
? ? ? ? ? ? //Console.WriteLine(myArray[1][3]); 第二行,第四個數,不存在,會報錯:超出索引
? ? ? ? ? ? //數據輸出如下:
? ? ? ? ? ? for (int i = 0; i < myArray.Length; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("第"+(i+1)+"行");
? ? ? ? ? ? ? ? for (int j = 0; j < myArray[i].Length; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? Console.Write(myArray[i][j]+",");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? Console.WriteLine();
? ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? Console.ReadLine();
? ? ? ? }
C# 數組、多維數組(矩形數組)、鋸齒數組(交叉數組)
數組是變量的索引列表,可以在方括號中指定索引來訪問數組中的各個成員,其中索引是一個整數,從0開始。
一維數組
多維數組(矩形數組)
數組的數組(鋸齒數組)
數組必須在訪問之前初始化,數組的初始化有兩種方式,可以以字面的形式指定數組的內容,也可以使用new關鍵詞顯式的初始化數組;
?
| 1 2 3 | int[] arr = { 1, 2, 3 }; int[] arr1 =?new?int[3]; |
數組類型是從抽象基類型 Array 派生的引用類型。由于此類型實現了 IEnumerable ,因此可以對 C# 中的所有數組使用 foreach 迭代,foreach循環(huán)對數組內容進行只讀訪問,所以不能改變任何元素的值。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | int[] arr = { 1, 2, 3 }; ???????foreach?(var?item?in?arr) ???????{ ???????????Console.WriteLine(item); ???????} ???????Array arr2 = Array.CreateInstance(typeof(int), 3); ???????arr2.SetValue(1, 0); ???????arr2.SetValue(2, 1); ???????arr2.SetValue(3, 2); ???????foreach?(var?item1?in?arr2) ???????{ ???????????Console.WriteLine(item1); ???????} |
?多維數組是使用多個索引訪問其元素的數組,下面以二維數組為例:
?
| 1 2 3 4 5 6 7 8 | int[,] arr = { { 1, 2 }, { 4, 5 }, { 7, 8 } }; ?????????int[,] arr1 =?new?int[3, 2]; ?????????arr1[0, 0] = 1; ?????????arr1[0, 1] = 2; ?????????foreach?(var?item?in?arr) ?????????{ ?????????????Console.WriteLine(item); ?????????} |
?方括號中的第一個數字指定花括號對,第二個數字花括號中的元素
鋸齒數組:該數組中的每個元素都是另外一個數組,? 每行都有不同的元素個數,
?
int[][] a = new int[2][];a[0] = new int[3];a[1] = new int[4];int[][] b = new int[2][] { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6, 7 } };int[][] c = { new int[3] { 1, 2, 3 }, new int[4] { 4, 5, 6, 7 } };//鋸齒數組的訪問int[][] a = { new int[3] { 1, 2, 3 }, new int[4] { 4, 5, 6, 7 } };foreach (var item in a){foreach (var item1 in item){Console.WriteLine(item1);}}Console.WriteLine("----------------------------------");for (int i = 0; i < a.Length; i++){for (int j = 0; j < a[i].Length; j++){Console.WriteLine(a[i][j]);}}?
鋸齒數組的初始化:
-
數值數組元素的默認值設置為零,而引用元素的默認值設置為 null。
-
交錯數組是數組的數組,因此,它的元素是引用類型,初始化為?null。
?
總結
以上是生活随笔為你收集整理的二维数组,锯齿数组和集合 C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用 C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全局脚手架了解一下【fle-cli】
- 下一篇: CTF-i春秋网鼎杯第一场misc部分w