CSharp中列表(数组)相关的处理

遍历

在遍历的时候,我们尽量不要对List进行遍历,因为List的长度是可变的,如果遍历的时候发生改变,就会报错。

推荐在遍历前把List转为Array

1
2
3
4
OnlineUser[] onlineUserArr = ZCommonData.OnlineUserList.ToArray();
foreach (OnlineUser user in onlineUserArr)
{
}

子列表(分页)

对于列表

在C#中,你可以使用List<T>类的GetRange方法来获取一个列表的子列表。GetRange方法接受两个参数,第一个是子列表的起始索引,第二个是子列表的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// 获取从索引 2 开始的 3 个元素,也就是子列表 [3, 4, 5]
List<int> subList = numbers.GetRange(2, 3);

// 输出子列表的元素
foreach (int num in subList)
{
Console.Write(num + " ");
}

// 输出结果为 "3 4 5"
}
}

对于ObservableCollection

ObservableCollection<T>类并没有提供类似List<T>GetRange方法。

如果你想从一个ObservableCollection<T>中获取子集,你可以使用 LINQ 查询来实现。

以下是一个示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.ObjectModel;
using System.Linq;

class Program
{
static void Main()
{
ObservableCollection<int> numbers = new ObservableCollection<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// 获取从索引 2 开始的 3 个元素,也就是子集 [3, 4, 5]
ObservableCollection<int> subCollection = new ObservableCollection<int>(numbers.Skip(2).Take(3));

// 输出子集的元素
foreach (int num in subCollection)
{
Console.Write(num + " ");
}

// 输出结果为 "3 4 5"
}
}

在上述示例中,我们使用Skip方法跳过前两个元素,然后使用Take方法获取接下来的三个元素,从而创建了一个子集。

数组项处理

使用 LINQ 查询来实现将列表中的每个元素拼接一个固定字符串,并生成一个新的列表。

以下是一个示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
List<int> numbers = new List<int>
{
1,
2,
3
};
List<string> incrementedNumbers = numbers.Select(num => "num_" + num).ToList();

// 输出增加后的列表
foreach (string str in incrementedNumbers)
{
Console.Write(str);
}

数组展平

二维数组变成一维数组

SelectMany方法可以用于将多个集合合并成一个集合。

以下是一个示例:

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
using System;
using System.Linq;

class Program
{
static void Main()
{
int[,] twoDimensionalArray = new int[,]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

int[] oneDimensionalArray = twoDimensionalArray.Cast<int>().ToArray();

// 输出一维数组
foreach (int num in oneDimensionalArray)
{
Console.Write(num + " ");
}

// 输出结果为 "1 2 3 4 5 6 7 8 9"
}
}

在上述示例中,我们使用Cast<int>()方法将二维数组中的元素转换为int类型,然后使用ToArray方法将结果转换为一个一维数组。

数组聚合操作

数组聚合操作是指在数组中对元素进行计算或者合并的操作。

这些操作通常包括对数组中的元素进行求和、计算平均值、找到最大值或最小值等等。

在C#中,可以使用LINQ(Language Integrated Query)来执行各种数组聚合操作。

LINQ提供了一套丰富的操作符和方法,可以方便地对数组进行查询和转换。

以下是一些常见的数组聚合操作示例:

  1. 求和(Sum)
1
2
int[] numbers = { 1, 2, 3, 4, 5 };
int sum = numbers.Sum(); // 结果为 15
  1. 计算平均值(Average)
1
2
int[] numbers = { 1, 2, 3, 4, 5 };
double average = numbers.Average(); // 结果为 3
  1. 找到最大值(Max)
1
2
int[] numbers = { 1, 2, 3, 4, 5 };
int max = numbers.Max(); // 结果为 5
  1. 找到最小值(Min)
1
2
int[] numbers = { 1, 2, 3, 4, 5 };
int min = numbers.Min(); // 结果为 1
  1. 统计符合条件的元素个数(Count)
1
2
int[] numbers = { 1, 2, 3, 4, 5 };
int count = numbers.Count(n => n % 2 == 0); // 结果为 2,统计偶数的个数
  1. 检查是否存在符合条件的元素(Any)
1
2
int[] numbers = { 1, 2, 3, 4, 5 };
bool anyGreaterThanTen = numbers.Any(n => n > 10); // 结果为 False

这只是一些示例,实际上还有许多其他的数组聚合操作可以执行,具体取决于你的需求和使用场景。

LINQ 提供了丰富的方法和操作符来支持各种数据查询和转换操作。

查找元素索引

你可以使用 LINQ 查询或者循环来在列表中查找符合条件的索引。

以下是两种方法的示例:

方法一:使用 LINQ 查询

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
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };

int target = 5;

// 使用 LINQ 查询找到符合条件的索引
int index = numbers.FindIndex(n => n == target);

if (index != -1)
{
Console.WriteLine("找到了,索引为:" + index);
}
else
{
Console.WriteLine("未找到");
}
}
}

在上述示例中,我们使用了 List<T>.FindIndex 方法和一个 lambda 表达式来查找符合条件的元素索引。

方法二:使用循环

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
31
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };

int target = 5;
int index = -1;

for (int i = 0; i < numbers.Count; i++)
{
if (numbers[i] == target)
{
index = i;
break;
}
}

if (index != -1)
{
Console.WriteLine("找到了,索引为:" + index);
}
else
{
Console.WriteLine("未找到");
}
}
}

在这个示例中,我们使用一个简单的 for 循环来遍历列表,检查每个元素是否符合条件。

无论使用哪种方法,都可以在列表中查找符合条件的索引。如果找到了符合条件的元素,将返回该元素的索引;如果未找到,则返回一个指示未找到的值(通常是 -1)。

List判断是否包含

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System;
using System.Linq;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };

int numberToFind = 3;

bool containsNumber = numbers.Any(x => x == numberToFind);

if (containsNumber)
{
Console.WriteLine("列表包含数字 " + numberToFind);
}
else
{
Console.WriteLine("列表不包含数字 " + numberToFind);
}
}
}

项目实例

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
31
32
33
34
35
36
37
38
39
40
41
42
43
List<ClassStudentModel> allStudentList = ZCommonData.AllStuList;
List<OnlineUser> onlineUserList = ZJsonHelper.JsonToList<OnlineUser>(ZJsonHelper.ToJson(ZCommonData.OnlineUserList));
ObservableCollection<QuesSubMenu> submitUserList = _pageData.userList;
foreach (ClassStudentModel stu in allStudentList)
{
bool isSubmit = submitUserList.Any(submitUser => submitUser.userid == stu.studentid);
if (isSubmit)
{
//提交的学生
stu.status = 2;
}
else if (onlineUserList.Any(onlineUser => onlineUser.userid == stu.studentid))
{
//在线未提交
stu.status = 3;
}
else
{
//不在线
stu.status = 0;
}
}
string notOnline = string.Empty; //未出勤学生名单
string unsubmitted = string.Empty; //未提交学生名单
foreach (ClassStudentModel user in allStudentList)
{
if (user.status == 3) //上线未提交
{
unsubmitted += user.studentname + " ";
}
else if (user.status == 0)
{
notOnline += user.studentname + " ";
}
}
if (!string.IsNullOrWhiteSpace(notOnline) || !string.IsNullOrWhiteSpace(unsubmitted))
{
TxbNotOnline.Text = unsubmitted + "\n" + notOnline;
}
else
{
TxbNotOnline.Text = "暂无";
}

数组索引

C#数组对象自身没有获取索引的方法,要用Array.IndexOf方法。

要获取 C# 数组中某个元素的索引,你可以使用Array.IndexOf方法或者自己编写一个循环进行搜索。

使用Array.IndexOf方法的语法如下所示:

1
int index = Array.IndexOf(array, value);

其中,array是要搜索的数组,value是要查找的元素。如果找到了该元素,index将返回其在数组中的索引;如果未找到,index将返回-1。

以下是一个示例:

1
2
3
int[] numbers = { 2, 4, 6, 8, 10 };
int index = Array.IndexOf(numbers, 6);
Console.WriteLine("元素6的索引为:" + index);

输出结果为:

1
元素6的索引为:2

列表排序

单字段排序

随机排序

1
List<ClassStudentModel> randomUserList = allUserList.OrderBy(x => Guid.NewGuid()).ToList();

正序

1
List<ClassStudentModel> randomUserList = allUserList.OrderBy(x => x.num).ToList();

倒序

1
List<ClassStudentModel> randomUserList = allUserList.OrderByDescending(x => x.num).ToList();

多次排序

随机排序后再按字段正序

1
List<ClassStudentModel> randomUserList = allUserList.OrderBy(x => Guid.NewGuid()).ToList().OrderBy(x => x.num).ToList();

随机排序后再按字段倒序

1
List<ClassStudentModel> randomUserList = allUserList.OrderBy(x => Guid.NewGuid()).ToList().OrderByDescending(x => x.num).ToList();

多字段同时排序

两个字段排序 先按Name正序再按num正序

1
List<ClassStudentModel> randomUserList = allUserList.OrderBy(x => x.Name).ThenBy(x => x.num).ToList();

两个字段排序 先按Name正序再按num倒序

1
List<ClassStudentModel> randomUserList = allUserList.OrderBy(x => x.Name).ThenByDescending(x => x.num).ToList();