集合和數組的差別:
數組:同一類型,固定長度
集合:不同類型,不固定長度
?
一、普通集合(弱類型)
1、ArryList 使用集合首先要引用命名空間。
或者在ArryList上右鍵找“解析”。
?
?
2、集合的定義: ArryList ?arr =new ArryList();
3、賦值:
arr.Add("值/變量"); //object類型
object類型:所有類型的基礎類型(基類)
4、獲取個數:
arr.Count;
5、取值:
arr[索引] ? ? 和數組一樣。
?
6、打印:
1)for(int i=0;i<arr.Count;i++)
{
? ? ? ? Console.WriteLine(arr[i]); ? //其中i是索引值,可以打印任何類型。
}
2) foreach(int i in arr)
{
? ? ? ?Console.WriteLine(i); ? ? ? ?//其中i是int類型,foreach用于都是同一類型的打印。
}
7、插隊:
arr.Insert(索引,值/變量); ? ? ? ? //將索引位置換位插入的值/變量,將索引位置原來的值/變量往后推一位。
8、移除:
arr.Remove(值); ? ? ? ? ? ? ? ? ?//移除的是一個值,從0開始只移除第一個匹配項。
arr.RemoveAt(索引); ? ? ? ? ? ?//移除的是索引位置對應的值
9、反轉:
arr.Reverse(); - 全部反轉
arr.Reverse(索引,個數); - 指定反轉 ? ? ? ? //從第幾個開始反轉,反轉幾個
10、清空:
arr.Clear();
二、泛型集合List(強類型) ? 固定類型,不固定長度
命名空間:using System.Collections.Generic;
?
List<T> ? ? ? ? ? ? ? ? ? ? ? ? ? // T:泛型,T可以為任何類型。
List<int> slist = new List<int>();
?
所有的操作同普通集合,都一樣!!!!!!!!
?
?
三、哈希表集合
注意命名空間
哈希表集合:弱類型
Hashtable hs = new Hashtable();
1、賦值
? ? ? ?// (鍵, 值) ? ? ? 鍵是object類型,即所有類型都可以。
hs.Add(1,"呵呵");
hs.Add("aaa","哈哈");
2、取值
Console.Write(hs[1]);
foreach(string s in hs.values)
{
? ? ? Console.write(s);
}
?
四、字典
?
字典:強類型
Dictionary<int, string> dic = new Dictionary<int, string>(); ? ? ? ? ? // ?<鍵,值>
?dic.Add(鍵,值) ? ? ? ? ? // 賦值里的鍵、值必須與定義的鍵、值是同類型
?
五、特殊集合(隊列集合、棧橋集合)
隊列集合:先進去的先出來
Queue ?q =new queue(); ? ? ? ? ? ? //Queue 右鍵解析一下
1、賦值
q.Enqueue("aaa");
q.Enqueue(111);
2、取值
Console.write(q.Dequeue()); ? ? ? //括號內沒有參數 ? ? 輸出"aaa"
?
棧橋集合:先進去的后出來,后進去的先出來。
Stack st =new Stack();
1、賦值
st.Push("aaa");
st.Push(111);
2、取值
Console.write(st.Pop()); ? ? ? ? ? ?//括號內沒有參數 ? ?輸出111
?
六、結構體
結構體:用戶自定義類型
定義位置:定義在Main函數的外面,類的里面
1、定義格式:
struct 自定義名字
{
public 數據類型 名字;
public 數據類型 名字;
...
...
}
?
?
2、聲明實例化:
結構體類型 s = new 結構體類型();
Student s = new Student();?
賦值:
s.???=???
s.???=????
取值:
s.???
?
作業題
結構體練習題:
“請輸入錄入學生的個數:”
記錄學生信息,需要輸入
“請輸入第1個學生的學號”
“請輸入第1個學生的姓名”
“請輸入第1個學生的生日”
“請輸入第1個學生的成績”
打印格式如下:
================學生信息展示===================
s001 張三 2000年1月1日 17 90
s002 李四 2000年1月1日 17 80
using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace 練習題講解 {class Program{struct Student{public string code;public string name;public DateTime birthday;public double score;}static void Main(string[] args){#region 錄入學生信息List<Student> list = new List<Student>(); //定義一個泛型集合 Console.Write("請輸入學生個數:");int count = Convert.ToInt32(Console.ReadLine());for (int i = 1; i <= count; i++){Student s = new Student();Console.Write("請輸入第" + i + "個學生的學號:");s.code = Console.ReadLine();Console.Write("請輸入第" + i + "個學生的姓名:");s.name = Console.ReadLine();Console.Write("請輸入第" + i + "個學生的生日:");s.birthday = Convert.ToDateTime(Console.ReadLine());Console.Write("請輸入第" + i + "個學生的成績:");s.score = Convert.ToDouble(Console.ReadLine());list.Add(s); //將填完的學生信息保存到準備好的集合里 }#endregion#region 冒泡排序計算for (int i = 0; i < list.Count - 1; i++){for (int j = i + 1; j < list.Count; j++){if (list[i].score < list[j].score){Student sss = list[i]; //如果i成績小于j成績,則將兩者全部調換位置,不是只調換分數list[i] = list[j];list[j] = sss;}}}#endregionConsole.WriteLine("================學生信息展示===================");foreach (Student s in list){int age = DateTime.Now.Year - s.birthday.Year;//用現在時間的年份減去生日的年份等于年齡。 Console.WriteLine(s.code + "\t" + s.name + "\t" + s.birthday.ToString("yyyy年MM月dd日") + "\t" + age + "\t" + s.score);}Console.ReadLine();}} }
?
?七、枚舉
用戶自定義類型 ? ? 在Main函數外邊,在類里邊。
用來統一數據格式的
enum Week
{
星期一,
星期二,
星期三,
星期四,
星期五,
星期六,
星期日
}
?