0%

【C#】List、Dictionary以及两种筛选算法


List与Dictionary都在System.Collections.Generic

①List

List是C#中的一种链表,它与数组类型相似性极高,比如使用时都需要先实例化、调用时都是根据索引、能够包含所有类等等。与数组类型不同的是,如果对IList 类的类型 T 使用引用类型,则两个类的行为是完全相同的。但是,如果对类型 T 使用值类型,则需要考虑实现和装箱问题。而添加到数组中的任何引用或值类型都将隐式地向上强制转换为 Object。如果项是值类型,则必须在将其添加到列表中时进行装箱操作,在检索时进行取消装箱操作。强制转换以及装箱和取消装箱操作都会降低性能;在必须对大型集合进行循环访问的情况下,装箱和取消装箱的影响非常明显。而且List在后期也可以增减元素,而不是像数组那样一旦实例化便不可增减。于是,为了性能与后期编程考虑,存储引用类型应偏向于使用List而不是数组。

创建一个List:

1.List<数据类型> 链表名=new List<数据类型>;

也可以在实例化的时候赋初始值,如

2.List<数据类型> 链表名=new List<数据类型>{数据1,数据2,……};
3.List<数据类型> 链表名=new List<数据类型>(声明数据量类型的数组);

如:

1
2
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
List testList = new List(temArr);

为List添加值

1.list.Add(值)

2.创建List的2、3方法 如:

1
2
3
4
5
6
7
8
9
10
11
List list=new list();
list.Add(1);
list.Add(2);```
<h2>显示List的值</h2>
<h6>1.显示特定值:</h6>

```csharp
ist list=new list();
list.Add(1);
list.Add(2);
Console.WriteLine(list[0]);
2.遍历List:
1
2
3
4
ist list=new list();
list.Add(1);
list.Add(2);
foreach(int x in list)Console.WriteLine(x);

删除List中的值

1.List. Remove(T item) 删除一个值

1
mList.Remove("Hunter");
  1. List. RemoveAt(int index); 删除下标为index的元素
1
mList.RemoveAt(0);
  1. List. RemoveRange(int index, int count); 从下标index开始,删除count个元素
1
mList.RemoveRange(3, 2);

4.List.Clear() 清空所有元素

②Dictionary

Dictionary与List功能相似,但是Dictionary可以同时对应任意两种变量,而不是List中的索引对变量。可以体现为从一组键(Key)到一组值(Value)的映射。 创建一个Dictionary:

Dictionary<键类型,值类型> 字典名=new Dictionary<键类型,值类型>;

具体添加、删除可以类比List。

③两种筛选算法

1.分类筛选:

对于这样一组数:1,2,3,4,5,6,2,3,4,5,6,7,8,9,3,4,5,6,7,8,9,9,9,9 以每个数作为分类标准,查找有几个数,以及每个数重复了几次:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int protect = 0;
string[] a = new string[] { "1", "2", "3", "4", "5", "6", "2", "3", "4", "5", "6", "7", "8", "9", "3", "4", "5", "6", "7", "8", "9", "9", "9", "9" };
Dictionary<int, string> dic = new Dictionary<int, string>();
for (int i = 0; i < a.Length; i++)
{
dic.Add(i, a[i]);
}
for (int x = 0; x < a.Length; x++)
{
int num = 0;
if (dic.ContainsKey(x))
{
for (int y = x; y < a.Length; y++)
{
if (dic.ContainsKey(y))
{
if (dic[x] == dic[y])
{
num++;
if (x != y) dic.Remove(y);
}
}
}
}
if (num > protect)
{
Console.Write(dic[x] + ":");
Console.WriteLine(num);
}
}

当数字的个数多于protect规定的值时,才显示这个数,可以作为一种可选筛选标准。 运行结果如图:

2.随机数筛选

一次产生多个随机数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public List Generate(int max,int min)
{
List result = new List();
int temp;
while (result.Count <Settingdata.Default.num)
{
temp = GetRandom(min, max);
if (!result.Contains(temp))
{
result.Add(temp);
}
}
return result;
}

参数min为最小值(包括),max为最大值(包括) 这里的GetRandom(min, max)是一种得到随机数的方法,可以是普通的以时间为seed的方法,也可以是RNG等多种方法 下面示例较为随机的使用RNG得到随机数的方法

1
2
3
4
5
6
7
8
9
10
11
12
13
public static int GetRandom(int minVal, int maxVal)
{
//这样产生0 ~ 100的强随机数(不含100)
int m = maxVal - minVal;
int rnd = int.MinValue;
decimal _base = (decimal)long.MaxValue;
byte[] rndSeries = new byte[8];
System.Security.Cryptography.RNGCryptoServiceProvider rng= new System.Security.Cryptography.RNGCryptoServiceProvider();
rng.GetBytes(rndSeries);
long l = BitConverter.ToInt64(rndSeries, 0);
rnd = (int)(Math.Abs(l) / _base * m);
return minVal + rnd;
}

参数minVal为最小值(包括),maxVal为最大值(包括)