使用哪种集合类型?

发布于 2024-09-14 05:25:15 字数 493 浏览 3 评论 0原文

我有一个场景,我有一个类列表,我想混淆顺序。例如:

private List<Question> myQuestions = new List<Question>();

因此,鉴于现在填充了一组数据,我想混合顺序。我的第一个想法是创建一个从 1 到 myQuestions.Count 的整数集合,为每个问题随机分配一个,然后按顺序循环遍历它们;但是,我似乎找不到合适的集合类型来用于此目的。我的意思的一个例子是这样的:

for (int i = 0; i <= myQuestions.Count -1; i++) 
    tempCollection[i] = myQuestions[rnd.Next(myQuestions.Count-1)];

但我不确定 tempCollection 应该是什么 - 它只需要是一个我可以在使用时删除的单个值。有人对使用哪种类型有任何建议,或者有更好的方法吗?

I have a scenario where I have a list of classes, and I want to mix up the order. For example:

private List<Question> myQuestions = new List<Question>();

So, given that this is now populated with a set of data, I want to mix up the order. My first thought was to create a collection of integers numbered from 1 to myQuestions.Count, assign one at random to each question and then loop through them in order; however, I can’t seem to find a suitable collection type to use for this. An example of what I mean would be something like this:

for (int i = 0; i <= myQuestions.Count -1; i++) 
    tempCollection[i] = myQuestions[rnd.Next(myQuestions.Count-1)];

But I’m not sure what tempCollection should be – it just needs to be a single value that I can remove as I use it. Does anyone have any suggestions as to which type to use, or of a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昔日梦未散 2024-09-21 05:25:15

我建议您将结果复制到新的 List 中,然后随机播放该列表。

但是,我会使用 Fisher-Yates shuffle 而不是那个你已经给了这里。此站点上有大量 C# 示例。

例如,您可能会这样做:

// Don't create a new instance of Random each time. That's a detail
// for another question though.
Random rng = GetAppropriateRandomInstanceForThread();

List<Question> shuffled = new List<Question>(myQuestions);
for (int i = shuffled.Count - 1; i > 0; i--)
{
    // Swap element "i" with a random earlier element it (or itself)
    int swapIndex = rng.Next(i + 1);
    Question tmp = shuffled[i];
    shuffled[i] = shuffled[swapIndex];
    shuffled[swapIndex] = tmp;
}

I suggest you copy the results into a new List<Question> and then shuffle that list.

However, I would use a Fisher-Yates shuffle rather than the one you've given here. There are plenty of C# examples of that on this site.

For example, you might do:

// Don't create a new instance of Random each time. That's a detail
// for another question though.
Random rng = GetAppropriateRandomInstanceForThread();

List<Question> shuffled = new List<Question>(myQuestions);
for (int i = shuffled.Count - 1; i > 0; i--)
{
    // Swap element "i" with a random earlier element it (or itself)
    int swapIndex = rng.Next(i + 1);
    Question tmp = shuffled[i];
    shuffled[i] = shuffled[swapIndex];
    shuffled[swapIndex] = tmp;
}
神魇的王 2024-09-21 05:25:15

您可以使用 Linq 并按随机值排序:

List<string> items = new List<string>(); 
items.Add("Foo");
items.Add("Bar");
items.Add("Baz");

foreach (string item in items.OrderBy(c => Guid.NewGuid()))
{
    Console.WriteLine(item);
}

You could use Linq and order by a random value:

List<string> items = new List<string>(); 
items.Add("Foo");
items.Add("Bar");
items.Add("Baz");

foreach (string item in items.OrderBy(c => Guid.NewGuid()))
{
    Console.WriteLine(item);
}
深海里的那抹蓝 2024-09-21 05:25:15

temp 集合应与 myQuestions 类型相同。

我还建议更改您的代码:

for (int i = 0; i <= myQuestions.Count -1; i++)  

for (int i = 0; i < myQuestions.Count; i++)  

做同样的事情,但这是大多数程序员的做法,因此它将使您的代码更易于阅读。

temp Collection should be same type as myQuestions.

I would also suggest a change in your code:

for (int i = 0; i <= myQuestions.Count -1; i++)  

to

for (int i = 0; i < myQuestions.Count; i++)  

Does the same thing, but this is how most programers do it so it will make your code simpler to read.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文