列表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
泛型:通过参数化类型来实现在同一份代码上操作多种数据类型。利用“参数化类型”将类型抽象化,从而实现灵活的复用。
泛型类定义:定义一个泛型类就是指的是,定义一个类,这个类中某些字段的类型是不确定的,这些类型可以在类构造的时候确定下来,举例:
创建一个类处理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);}}
}
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] + " ");}}}}
}
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}}}
}