在 C# 中混排列表
在本教程中,我们将讨论在 C# 中对列表进行混排的方法。
在 C# 中使用 Linq 混排列表
语言集成查询或 Linq
提供了一种在 C# 中集成查询功能的方法。Linq 提供的功能类似于 C# 中的 SQL。我们可以使用 Linq 将列表随机化。以下代码示例向我们展示了如何在 C# 中使用 Linq 来整理列表。
using System;
using System.Collections.Generic;
using System.Linq;
namespace shuffle_list {
class Program {
static void Main(string[] args) {
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5 };
var rnd = new Random();
var randomized = list1.OrderBy(item => rnd.Next());
foreach (var value in randomized) {
Console.WriteLine(value);
}
}
}
}
输出:
1
4
5
3
2
我们首先初始化列表 list1
,然后使用 C# 中 Linq 的 random.next()
函数和 OrderBy()
函数对列表 list1
进行混洗。上面的方法还可以用于在 C# 中对对象列表进行随机组合。以下代码示例向我们展示了如何在 C# 中使用 Linq 来整理对象列表。
using System;
using System.Collections.Generic;
using System.Linq;
namespace shuffle_list {
public class Person {
string name;
public Person(string name) {
Name = name;
}
public string Name {
get => name;
set => name = value;
}
}
class Program {
static void Main(string[] args) {
List<Person> list1 = new List<Person>();
list1.Add(new Person("Person 1"));
list1.Add(new Person("Person 2"));
list1.Add(new Person("Person 3"));
list1.Add(new Person("Person 4"));
list1.Add(new Person("Person 5"));
var rnd = new Random();
var randomized = list1.OrderBy(item => rnd.Next());
foreach (var value in randomized) {
Console.WriteLine(value.Name);
}
}
}
}
输出:
Person 5
Person 2
Person 1
Person 3
Person 4
我们使用 C# 中的 Linq 对 Person
类的对象列表进行了混排。
使用 C# 中的 Fisher-Yates Shuffle 算法混排列表
Fisher-Yates Shuffle 算法在 C# 中对有限数据结构进行混排。Fisher-Yates 混排算法在 C# 中提供了无偏混排。它将列表中的每个元素顺序存储到列表中的随机索引中。下面的编码示例向我们展示了如何使用 C# 中的 Fisher-Yates 混排算法来混排列表。
using System;
using System.Collections.Generic;
using System.Linq;
namespace shuffle_list {
static class ExtensionsClass {
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list) {
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
class Program {
static void Main(string[] args) {
List<int> list1 = new List<int>() { 1, 2, 3, 4, 5 };
list1.Shuffle();
foreach (var value in list1) {
Console.WriteLine(value);
}
}
}
}
输出:
5
1
4
3
2
我们使用 C# 中的 Fisher-Yates 混排算法对整数 list1
的列表进行了混排。我们创建了一种扩展方法,用于在 C# 中实现 Fisher-Yates 算法。要创建扩展方法,我们必须在另一个称为 ExtensionClass
的 static
类中定义 Shuffle()
函数。如下面的代码所示,也可以使用相同的算法对对象列表进行混排。
using System;
using System.Collections.Generic;
using System.Linq;
namespace shuffle_list {
public class Person {
string name;
public Person(string name) {
Name = name;
}
public string Name {
get => name;
set => name = value;
}
}
static class ExtensionsClass {
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list) {
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
class Program {
static void Main(string[] args) {
List<Person> list1 = new List<Person>();
list1.Add(new Person("Person 1"));
list1.Add(new Person("Person 2"));
list1.Add(new Person("Person 3"));
list1.Add(new Person("Person 4"));
list1.Add(new Person("Person 5"));
list1.Shuffle();
foreach (var value in list1) {
Console.WriteLine(value.Name);
}
}
}
}
输出:
Person 1
Person 4
Person 2
Person 5
Person 3
我们使用 C# 中的 Fisher-Yates 算法对 Person
类的对象列表进行了混排。我们创建了一种扩展方法,用于在 C# 中实现 Fisher-Yates 算法。要创建扩展方法,我们必须在另一个称为 ExtensionClass
的 static
类中定义 Shuffle()
函数。
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径