2016-04-23 20:46:26
一.二維數組:
一維數組----豆角
二維數組----表格
1)定義:
一維數組:
數據類型[] 數組變量名 = new 數據類型[數組長度];
數據類型[] 數組變量名 = new 數據類型[數組長度]{1,2,3....};
2)二維數組:
數據類型[,] 數組變量名 = new 數據類型[行數,列數];
int[,] a = new int[3,4];
賦值:
a[行下標,列下標] = 值 下標都是從0開始的
取值:
a[行下標,列下標]
☆.題目:一個班6個人,從鍵盤輸入每個學號語文,數學,外語成績(不需輸入學號)。輸出:學生成績表(包括每個人的總分),每科的平均分。
附加1:試着,把不及格的用紅字顯示。
附加2:試着按照總分排序,顯示名次出來。
int [,]a=new int [6,5]; for (int i = 0; i < 6;i++ ) { Console.WriteLine("輸入第{0}位同學的語文成績:",i+1); int 語文成績 = Convert.ToInt32(Console .ReadLine ()); Console.WriteLine("輸入第{0}位同學的數學成績:", i + 1); int 數學成績 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("輸入第{0}位同學的英語成績:", i + 1); int 英語成績 = Convert.ToInt32(Console.ReadLine()); a[i, 0] = 語文成績; a[i,1]=數學成績 ; a[i, 2] = 英語成績; a[i,3] = 語文成績 + 數學成績 + 英語成績; } for (int i = 0; i < 6;i++ ) { for (int j = i; j < 5;j++ ) { if (a[i,3]>a[j+1,3]) { int v = a[i, 3]; a[i, 3] = a[j + 1, 3]; a[j + 1, 3] = v; int m = a[i, 0]; a[i, 0] = a[j + 1, 0]; a[j + 1, 0] = m; int n = a[i, 1]; a[i, 1] = a[j + 1, 1]; a[j + 1, 1] = n; int b= a[i, 2]; a[i, 2] = a[j + 1, 2]; a[j + 1, 2] = b; } } } int sum語文 = 0, sum數學 = 0, sum英語 = 0, avg語文, avg數學, avg英語; Console.Write("語文\t數學\t英語\t總分\t排序"); Console.WriteLine(); for (int i = 0; i < 6;i++ ) { Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}", a[i, 0], a[i, 1], a[i, 2], a[i, 3],i+1); sum語文 = sum語文 + a[i, 0]; sum數學 = sum數學 + a[i, 1]; sum英語 = sum英語 + a[i, 2]; } avg語文 = sum語文 / 6; avg數學 = sum數學 / 6; avg英語 = sum英語 / 6; Console.WriteLine("平均分:語文{0}\t數學{1}\t英語{2}",avg語文 ,avg數學 ,avg英語 );
二.鋸齒數據,數組的數組。
定義:
第一步:定義大數組
數據類型[][] a = new 數據類型[行數][];
第二步:定義小數組
數據類型[] a1 = new 數據類型[列數];
數據類型[] a2 = new 數據類型[列數];
......
第三步:把小數組放到大數組中
a[0] = a1;
a[1] = a2;
....
代碼:
int[][] a = new int[3][]; int[] a0 = new int[] { 1, 2, 3, 4, 5 }; int[] a1 = new int[] { 6, 7, 8 }; int[] a2 = new int[] { 9, 10 }; a[0] = a0; a[1] = a1; a[2] = a2; //顯示 for (int i = 0; i < a.Length; i++)//a.length=3 { for (int j = 0; j < a[i].Length; j++) { Console.Write(a[i][j] + "\t"); } Console.WriteLine(); }
★ 拓展
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;
三.集合
1、ArrayList 鏈表,沒有長度限制,可以隨時添加或刪除元素。
需要在前面加上:using System.Collections;
定義:
ArrayList a = new ArrayList();
操作:
a.Add(數據):添加
a.Insert(索引號,數據):插入
a.RemoveAt(索引號):刪除
a.Count 集合中元素的個數
取值:
a[下標]
取出來的值需要進行強制轉換。
代碼舉例
ArrayList a = new ArrayList(); a.Add(110); a.Add(120); a.Add(500); a.Add("湯圓"); a.Add(true ); a.Insert(1,"chihuo");//插入第2條為“chihuo” a.RemoveAt(3);//刪除第四行 500 for (int i = 0; i < a.Count;i++ ) { Console.WriteLine(a[i]); }
結果顯示
2、List<類型> 鏈表,,沒有長度限制,可以隨時向時添加或刪除元素。只能放指定類型的數據,取出來也不用強制轉換。
定義
List<類型> 變量名 = new List<類型>();
List<int> a = new List<int>();
操作:
a.Add(數據):添加
a.Insert(索引號,數據):插入
a.RemoveAt(索引號):刪除
a.Count 集合中元素的個數
a.Sort(); 排序
a.Reverse();反轉
取值
a[索引號]
示例:
List <int > a=new List<int> (); a.Add(10); a.Add(100); a.Add(1000); a.Add(500); a.Insert(2, 123); a.RemoveAt(3); for (int i = 0; i < a.Count;i++ ) { Console.WriteLine(a[i]); }
結果顯示
集合中的集合:
List<List<int>> a = new List<List<int>>(); List<int> a1 = new List<int>(); a1.Add(10); a1.Add(12); a1.Add(15); List<int> a2 = new List<int>(); a2.Add(20); a2.Add(23); a2.Add(28); a.Add(a1); a.Add(a2);
3.Dictionary<key,value>字典或哈希表
定義
Dictionary<int,string> a = new Dictionary<int,string>();
操作:
a.Add(鍵值,數據);
a.Remove(鍵值);
a.Count;
取值:
a[鍵值]
代碼示例
4、棧,隊列
棧:先進后出,不能隨機取其中任意一個值。
Stack<數據類型> a = new Stack<數據類型>();
a.Push(值);
數據類型 變量名 = a.Pop();
隊例:先進先出,不能隨機取其中任意一個值。
Queue<int> a = new Queue<int>();
a.Enqueue(值);
數據類型 變量 = a.Dequeue();
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。