集合类 列表List
列表List的创建和使用
1.创建列表(列表可以存储任何类型的数据,在创建列表对象的时候首先要指定你要创建的这个列表要存储声明类型的)
List<int> scoreList = new List<int>();
new List<int>(){123}
new List<string>(){"one","two"}
var scoreList = new List<int>();
往列表中插入数据
list.Add(12);
using System;
using System.Collections.Generic;
namespace 列表的创建
{
class Program
{
static void Main(string[] args)
{
List<int> list = new List<int>() { 1,2,3,4};
list.Add(8);
for(int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
}
}
}
列表内部数据
1,列表内部数据是使用数组进行的存储,一个空的列表内部会有一个长度为0的数组,当给列表中添加元素的时候,列表的容量会扩大为4,如果添加第5个的时候,列表的大小会重新设置为8,如果添加第9个元素,列表容量会扩大为16,依次增加。当列表的中的容量发生改变的时候,它会创建一个新的数组,使用Array.Copy()方法将旧数组中的元素复制到新数组中。为了节省时间,如果事先知道要存储的数据的个数,就可以利用列表的构造函数指定列表的容量大小,比如下面的
List<int> intlist = new List<int>(10);创建了一个初始容量为10的列表,当容量不够用的时候,每次都会按照原来容量的2倍进行扩容。
我们可以通过Capacity属性获取和设置容量intList.Capacity = 100;
⒉,注意容量和列表中元素个数的区别,容量是列表中用于存储数据的数组的长度通过Cap a c it y获取,列表中的元素是我们添加进去需要管理的数据,通过Count获取
列表的遍历
1.for循环
for(int i = 0;i <list.Count;i++){
//循环体
}
2.foreach遍历
foreach(int temp in list){ //依次取得list中的每一个元素赋值给temp,并执行循环体
//循环体
}
操作列表的属性和方法
1.Capacity获取容量大小
2.Add()方法添加元素
3.Insert()方法插入元素
4.[index]访问元素
5.Count属性访问元素个数
6.RemoveAt()方法移除指定位置的元素
7.IndexOf()方法取得一个元素所在列表中的索引位置
LastlndexOf()上面的方法是从前往后搜索,这个是从后往前搜索,搜索到满足条件的就停止
上面的两个方法,如果没有找到指定元素就返回-1
8.Sort()对列表中是元素进行从小到大排序
class Program
{
static void Show(List<int> list)
{
foreach(int temp in list)
{
Console.Write(temp + " ");
}
}
static void Main(string[] args)
{
List<int> list = new List<int>() { 1, 2, 3, 7, 4, 9, 5, 1, 5 };
Show(list);
Console.WriteLine();
//1.Capacity
Console.WriteLine(list.Capacity);
Console.WriteLine();
//2.Add
list.Add(11);
Show(list);
Console.WriteLine();
//3.Insert
list.Insert(3, 98);
Show(list);
Console.WriteLine();
//4.Count
Console.WriteLine(list.Count);
Console.WriteLine();
//5.RemoveAt()
list.RemoveAt(3);
Show(list);
Console.WriteLine();
//6.IndexOf()
Console.WriteLine(list.IndexOf(5));
Console.WriteLine();
//LastindexOf()
Console.WriteLine(list.LastIndexOf(5));
Console.WriteLine();
//7.Sort()
list.Sort();
Show(list);
}
}
泛型
定义一个泛型类指的是,定义一个类,这个类中某些字段的类型是不确定的,这些类型可以在类构造的时候确定下来
class Program
{
static void Main(string[] args)
{
Calculation<int> a = new Calculation<int>(1,2);
Console.WriteLine(a.Sum());
Calculation<double> b = new Calculation<double>(1.9,2.2);
Console.WriteLine(b.Sum());
}
}
class Calculation<T>
{
private T a;
private T b;
public Calculation(T a, T b)
{
this.a = a;
this.b = b;
}
public T Sum()
{
dynamic num1 = a;
dynamic num2 = b;
dynamic ret = num1 + num2;
return (T)ret;
}
编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性。
tostring()方法
返回的是这个类的名字
模拟实现List
class MyList<T>
{
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>=count || index < 0)
{
throw new System.ArgumentOutOfRangeException("参数超出范围");
}
return data[index];
}
set
{
data[index] = value;
}
}
public void Insert(int index, T temp)
{
if (index < 0 || index > count)
{
throw new System.ArgumentOutOfRangeException("参数超出范围");
}
for (int i = count - 1; i > index - 1; i--)
{
data[i + 1] = data[i];
}
data[index] = temp;
count++;
}
public void RemoveAt(int index)
{
if (index < 0 || index > count)
{
throw new System.ArgumentOutOfRangeException("参数超出范围");
}
for (int i = index; i < count; i++)
{
data[i] = data[i + 1];
}
count--;
}
public int IndexOf(T item)
{
int index = -1;
for(int i = 0; i < count; i++)
{
if(item.Equals(data[i]))
{
index = i;
break;
}
}
return index;
}
public int LastindexOf(T item)
{
int index = -1;
for (int i = count-1; i > -1; i--)
{
if (item.Equals(data[i]))
{
index = i;
break;
}
}
return index;
}
public void Sort()
{
Array.Sort(data, 0, count);
}
}