转战C#---day7
创始人
2025-05-31 01:05:33
  • 集合类–列表List
    列表List的主要作用是将某个数组进行包装,可以很方便的让我们进行存储数据以及数据的一些常见的操作已经实现了可以直接调用方法进行排序、查找、插入等一些操作。

列表List的创建和使用:
1、创建列表(列表可以存储任何类型的数据,在创建列表对象的时候首先要指定你要创建的这个列表要存储什么类型的)(泛型)
列表可以随意添加数据,数组不能。

List scoreList = new List();
new List(){1,2,3}
new List(){"one","two"}
var scoreList = new List();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{      static void Main(string[] args){List list = new List() {321,123,32};//List list = new List();list.Add(900);list.Add(3624);list[3] = 9;Console.WriteLine(list[3]);for (int i = 0; i < list.Count; i++){Console.WriteLine(list[i]);}}}
}

2、关于列表的内部数据管理:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{      static void Main(string[] args){List list = new List();Console.WriteLine(list.Count+":"+list.Capacity);list.Add(12);Console.WriteLine(list.Count + ":" + list.Capacity);list.Add(12);list.Add(12);list.Add(12);Console.WriteLine(list.Count + ":" + list.Capacity);list.Add(12);Console.WriteLine(list.Count + ":" + list.Capacity);//list.Capacity是每4个一增长。容量不够就增长4个空间每次。List list1 = new List(50);//也可以自定义容量。接下去增长,空间直接为100,每50一增加。Console.WriteLine(list1.Count + ":" + list1.Capacity);list1.Add(34); list1.Add(341);foreach (int temp in list1){Console.WriteLine(temp);}}}
}

3、列表的一些常见操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{static void ShowList(Listlist){foreach (int temp in list){Console.Write(temp + " ");}Console.WriteLine();}static private void RemoveAll(List list, int data){List listF = new List();//object的作用相当于是varlistF.Add(data);list.RemoveAll(it => listF.Contains(it));//移除所有和列表表尾增添的元素一样的元素。}static void Main(string[] args){List list = new List() {56,23,43,32,324,3234,32};//增删改查:Console.WriteLine(list.Capacity);list.Add(12);Console.WriteLine(list[2]);list.Insert(3,900);ShowList(list);list.Remove(32);//只会移除匹配的第一个数。ShowList(list);list.RemoveAt(2);//按索引的方式来移除数据。ShowList(list);RemoveAll(list,32);ShowList(list);Console.WriteLine(list.IndexOf(32));Console.WriteLine(list.IndexOf(100));//不存在的数,返回的索引是-1。list.Sort();ShowList(list);}}
}
 
  • 泛型–泛型类:

泛型:通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用。
泛型类定义:定义一个泛型类就是指的是,定义一个类,这个类中某些字段的类型是不确定的,这些类型可以在类构造的时候确定下来,举例:
创建一个类处理int类型和double类型的相加。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{static void Main(string[] args){//ClassA a = new ClassA(34, 12);//Console.WriteLine(a.GetSum());ClassA a = new ClassA(45, 65);Console.WriteLine(a.GetSum());ClassA b = new ClassA(45.1, 65.0);Console.WriteLine(b.GetSum());}class ClassA//Type int string double T可以代表任何类型。{//MyClass private T a;private T b;public ClassA(T a, T b){this.a = a;this.b = b;}public T GetSum(){//动态类型dynamic:In most cases, it functions like it has type object.dynamic num1 = a;dynamic num2 = b;dynamic result = num1 + num2;return (T)result;}}}
}

类中的ToString方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{static void Main(string[] args){Program p = new Program();//ToString()默认情况下就是输出这个类的完整路径的。Console.WriteLine(p.ToString());Console.WriteLine(p);//两个类是不能进行相加的,必须要转换成字符串类型才能进行相加。Program p1 = new Program();Program p2 = new Program();string str = p1.ToString() + p2.ToString();Student s = new Student(30,"小爱");Console.WriteLine(s);}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Student : object//默认继承object{private int age;private string name;public Student(int age, string name){this.age = age;this.name = name;}public override string ToString()//将ToString()方法进行重载。{string result = age + ":" + name;return result;}}
}

泛型方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{static void Main(string[] args){Console.WriteLine(GetSum(33,56));Console.WriteLine(GetSum(34.9,56));}public static T GetSum(T a, T b){dynamic num1 = a;dynamic num2 = b;return (T)(num1 + num2);}}
}
  • 创建自己的MyList列表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{static void Main(string[] args){MyList list = new MyList();//Console.WriteLine(list.Capacity);//获取容量list.Add(1);list.Add(2);list.Add(3);list.Add(4);list.Add(5);list.Add(4);//for (int i = 0; i < list.Count; i++)//{//    Console.Write(list[i] + " ");//}//Console.WriteLine(list.Count);//获取元素个数int temp1 = list[10];int temp2 = list[-1];//List l = new List();//int temp = l[-1];list.Insert(2, 100);for (int i = 0; i < list.Count; i++){Console.Write(list[i] + " ");}Console.WriteLine();list.RemoveAt(3);for (int i = 0; i < list.Count; i++){Console.Write(list[i] + " ");}Console.WriteLine();Console.WriteLine(list.IndexOf(4));Console.WriteLine(list.LastIndexOf(4));list.Sort();for (int i = 0; i < list.Count; i++){Console.Write(list[i] + " ");}Console.WriteLine();list.RemoveAll(4);Console.WriteLine();}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class MyList{private T[] data = new T[0];//data null//引用类型 new;//MyClass mc;private T[] Data = new T[0];private int count = 0;//元素个数  数据个数public int Capacity{get{return data.Length;}}public int Count{get{return count;}}public void Add(T item){if (data.Length == 0){data = new T[4];}//添加元素之前,先判断数组是否已满。if (data.Length == count){T[] temp = new T[count * 2];for (int i = 0; i < data.Length; i++){temp[i] = data[i];}data = temp;}data[count] = item;count++;}public T this[int index]{get{if (index < 0 || index > count-1){throw new ArgumentOutOfRangeException("索引参数超出范围了!");}return data[index];}set{data[index] = value;}}public void Insert(int index, T item){if (index < 0 || index > count - 1){throw new ArgumentOutOfRangeException("索引参数超出范围了!");}for (int i = count - 1; i > index-1; i--){data[i + 1] = data[i];}data[index] = item;count++;}public void RemoveAt(int index){if (index < 0 || index > count - 1){throw new ArgumentOutOfRangeException("索引参数超出范围了!");}for (int i = index + 1; i < count; i++){data[i - 1] = data[i];}count--;}public int IndexOf(T item){//ToString Equalsint index = -1;for (int i = 0; i < count; i++){if (item.Equals(data[i])){index = i;break;}}return index;}public int LastIndexOf(T item)//从后面开始遍历{//ToString Equalsint index = -1;for (int i = count - 1; i >= 0; i--){if (item.Equals(data[i])){index = i;break;}}return index;}public void Sort(){Array.Sort(data, 0, count);}public void RemoveAll(T item){for (int i = 0; i < count; i++){if (!item.Equals(data[i])){Console.Write(data[i] + " ");}}}}
}
  • Equals()类:
    不是所有的类型都可以通过==来进行比较,但所有类型都可以通过Equals()来比较。
    两个对象来进行比较时,需要
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Student{private int age;private string name;public Student(int age, string name){this.age = age;this.name = name;}public override bool Equals(object obj)//将Equals()方法进行重写。main里面传递的是student对象。{Student stu = (Student)obj;//强制类型转换成Student类if(age==stu.age && name==stu.name){return true;}return false;}public static bool operator == (Student s1, Student s2){if (s1.age == s2.age&&s1.name == s2.name){return true;}return false;}public static bool operator !=(Student s1, Student s2){bool result = s1 == s2;return !result;}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Test
{class Program{static void Main(string[] args){//equals//ToStrings Equals Objectint a = 12;int b = 34;int c = 12;Console.WriteLine(a.Equals(b));Console.WriteLine(a.Equals(c));string str1 = "xiaoai";string str2 = "123";string str3 = "xiaoai";Console.WriteLine(str1.Equals(str2));Console.WriteLine(str1.Equals(str3));Student stu1 = new Student(18,"xiao ai");Student stu2 = new Student(18, "xiao ai");Console.WriteLine(stu1.Equals(stu2));Console.WriteLine(stu1 == stu2);}}
}
  • 多线程:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;namespace Test
{class Program{       static void Main(string[] args){//Thread t = Thread.CurrentThread;//t.Name = "MainThread";//Console.WriteLine(t.Name);//这里使用的委托,将方法当做数据传递。//ThreadStart Start = new ThreadStart(ChildThreadMethod);//Thread ChildThread = new Thread(Start);//或者下面这样表示:Thread ChildThread1 = new Thread(new ThreadStart(ChildThreadMethod));ChildThread1.Start();Thread ChildThread2 = new Thread(new ThreadStart(ChildThreadMethod));ChildThread2.Start();//Console.WriteLine("Main Method");//Thread.Sleep(2000);//ChildThread1.Abort();//强制中止,不管程序有没有执行完毕。//ChildThread2.Abort();isRun = false;//标记中断int i = 10;while (i > 0){i--;Console.WriteLine("Main Method - 聊天中...");Thread.Sleep(1000);//线程休眠,1s = 1000ms}}private static bool isRun = true;private static void ChildThreadMethod(){int i = 10;while (isRun){i--;Console.WriteLine("ChildThread - 听歌中...");Thread.Sleep(1000);//线程休眠,1s = 1000ms}}}
}

相关内容

热门资讯

汽车摆件(汽车摆件正面朝哪摆放... 本篇文章极速百科给大家谈谈汽车摆件,以及汽车摆件正面朝哪摆放对应的知识点,希望对各位有所帮助,不要忘...
包茂高速(包茂高速公路封闭最新... 今天给各位分享包茂高速的知识,其中也会对包茂高速公路封闭最新消息进行解释,如果能碰巧解决你现在面临的...
丽江到攀枝花多少公里(丽江到攀... 今天给各位分享丽江到攀枝花多少公里的知识,其中也会对丽江到攀枝花多少公里路程进行解释,如果能碰巧解决...
东南是合资车吗(东南车是合资车... 本篇文章极速百科给大家谈谈东南是合资车吗,以及东南车是合资车吗对应的知识点,希望对各位有所帮助,不要...
滴滴代驾条件(滴滴代驾招聘条件... 本篇文章极速百科给大家谈谈滴滴代驾条件,以及滴滴代驾招聘条件对应的知识点,希望对各位有所帮助,不要忘...
高尔夫4发动机1.6(高尔夫4... 今天给各位分享高尔夫4发动机1.6的知识,其中也会对高尔夫4发动机5V图详细图解进行解释,如果能碰巧...
d4502次列车(d4525列... 今天给各位分享d4502次列车的知识,其中也会对d4525列车进行解释,如果能碰巧解决你现在面临的问...
朗动2016款(朗动2016款... 今天给各位分享朗动2016款的知识,其中也会对朗动2016款自动尊贵型进行解释,如果能碰巧解决你现在...
轻轨买票怎么网上订票(轻轨怎么... 本篇文章极速百科给大家谈谈轻轨买票怎么网上订票,以及轻轨怎么买票怎么网上购买对应的知识点,希望对各位...
神舟租车(神舟租车担保人需要承... 今天给各位分享神舟租车的知识,其中也会对神舟租车担保人需要承担啥进行解释,如果能碰巧解决你现在面临的...
amgc63s(amgc63s... 本篇文章极速百科给大家谈谈amgc63s,以及amgc63s二手车价格对应的知识点,希望对各位有所帮...
安全带怎么解开(飞机上的安全带... 本篇文章极速百科给大家谈谈安全带怎么解开,以及飞机上的安全带怎么解开对应的知识点,希望对各位有所帮助...
广州至上海高铁(广州至上海高铁... 今天给各位分享广州至上海高铁的知识,其中也会对广州至上海高铁途经站进行解释,如果能碰巧解决你现在面临...
本田杰德(本田杰德是几座的车)... 今天给各位分享本田杰德的知识,其中也会对本田杰德是几座的车进行解释,如果能碰巧解决你现在面临的问题,...
关于836路公交车路线的信息 ... 本篇文章极速百科给大家谈谈836路公交车路线,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站...
承载式好还是非承载式(承载式和... 本篇文章极速百科给大家谈谈承载式好还是非承载式,以及承载式和非承载式哪个耐用对应的知识点,希望对各位...
助力车需要驾驶证吗(助力车需要... 今天给各位分享助力车需要驾驶证吗的知识,其中也会对助力车需要什么证进行解释,如果能碰巧解决你现在面临...
张家口二手车(张家口二手车买卖... 本篇文章极速百科给大家谈谈张家口二手车,以及张家口二手车买卖个人出售对应的知识点,希望对各位有所帮助...
石家庄到杭州高铁(石家庄到杭州... 今天给各位分享石家庄到杭州高铁的知识,其中也会对石家庄到杭州高铁票多少钱进行解释,如果能碰巧解决你现...
北京到杭州(北京到杭州自驾游最... 本篇文章极速百科给大家谈谈北京到杭州,以及北京到杭州自驾游最佳路线怎么走对应的知识点,希望对各位有所...
氙气(氙气大灯寿命) 氙气 氙... 本篇文章极速百科给大家谈谈氙气,以及氙气大灯寿命对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔...
751路公交车路线(751路公... 今天给各位分享751路公交车路线的知识,其中也会对751路公交车路线多久一趟进行解释,如果能碰巧解决...
10款捷达(10款捷达伙伴) ... 今天给各位分享10款捷达的知识,其中也会对10款捷达伙伴进行解释,如果能碰巧解决你现在面临的问题,别...
汽车换机油多少钱(汽车机油5w... 今天给各位分享汽车换机油多少钱的知识,其中也会对汽车机油5w30 5w40 哪个好进行解释,如果能碰...
商业险多少钱(轿车商业险多少钱... 本篇文章极速百科给大家谈谈商业险多少钱,以及轿车商业险多少钱对应的知识点,希望对各位有所帮助,不要忘...
大庆到天津(大庆到天津航班时刻... 本篇文章极速百科给大家谈谈大庆到天津,以及大庆到天津航班时刻表对应的知识点,希望对各位有所帮助,不要...
汽车雨刷怎么拆卸(汽车雨刷拆卸... 今天给各位分享汽车雨刷怎么拆卸的知识,其中也会对汽车雨刷拆卸视频进行解释,如果能碰巧解决你现在面临的...
防冻液多少钱一桶(美孚防冻液多... 本篇文章极速百科给大家谈谈防冻液多少钱一桶,以及美孚防冻液多少钱一桶对应的知识点,希望对各位有所帮助...
过户(过户费一般多少钱) 过户... 本篇文章极速百科给大家谈谈过户,以及过户费一般多少钱对应的知识点,希望对各位有所帮助,不要忘了收藏本...
长春旅游景点一日游(长春一日游... 今天给各位分享长春旅游景点一日游的知识,其中也会对长春一日游旅游攻略进行解释,如果能碰巧解决你现在面...