我正在尝试以下代码来混杂ArrayList
-的元素questionsAndSeperators
。我有两种方法。
方法1:-
List<Question> questionList = this.questionsAndSeperators.Cast<Question>().ToList();
Random rng = new Random();
int questionCount = questionList.Count;
while (questionCount > 1)
{
questionCount--;
int index = rng.Next(questionCount + 1);
Question value = questionList[index];
questionList[index] = questionList[questionCount];
questionList[questionCount] = value;
}
方法2:-
ArrayList questionList = this.questionsAndSeperators;
Random rng = new Random();
int questionCount = questionList.Count;
while (questionCount > 1)
{
questionCount--;
int index = rng.Next(questionCount + 1);
object value = questionList[index];
questionList[index] = questionList[questionCount];
questionList[questionCount] = value;
}
这里Question
有一堂课。
方法 2工作正常并混杂 的元素questionsAndSeperators
,但方法 1无法混杂 的元素questionsAndSeperators
。方法1有什么问题??我在方法1中做错了吗?