尝试将随机生成的列表存储在列表中,然后再添加到列表中

时间:2015-04-10 04:54:10

标签: c#

这是我的代码的简化版本。基本上是怎么回事: 我有一个rng,它从外部列表中随机选择对象,并将一组随机的对象生成一个新的列表,其长度由用户选择。

即。用户从列表中随机选择4个,4个对象,放入新列表。

我需要将4个列表存储在新列表中,并且每次遍历该循环,直到原始列表中不再有对象为止。

即。选择4个obj,存储在studentGroupList [0]中,接下来4个,存储在studentGroupList [1]等中,直到外部列表为空。

我遇到的问题是我不知道如何正确地在列表中存储列表,我不知道如何迭代这些嵌套列表来显示对象,并在以后添加或修改它们,有条不紊地

执行简明说明:用户选择要存储的列表长度,(例如4) 从初始列表中选择并删除4个对象; 这4个对象存储在currentGroupList中 currentGroupList需要存储在studentGroupList的index [0]中 清除currentGroupList以存储4个随机对象。 循环直到初始列表为空。

能够根据需要添加和修改studentGroupList中的列表/遍历列表列表中的所有索引。

请帮忙!

        /
    public static void GroupListMaker(List<string> students, int numberOfStudents)
    {
        //User selects size of groups
        Console.WriteLine("Please enter a groups size: ");
        int groupSize = int.Parse(Console.ReadLine());

        //Creates rng
        Random rng = new Random();
        //First String for storing strings
        List<List<string>> studentGroupList = new List<List<string>>();
        //temporarily stores names in list for later storage in studentGroupList
        List<string> currentGroupList = new List<string>();

        int selector = numberOfStudents - 1;
        int groupNumber = 1;

        //while there are students left to sort
        while (students.Count > 0)
        {
            //Assign random student to variable
            string thisStudent = students[rng.Next(selector)];
            //Add variable to current list
            currentGroupList.Add(thisStudent);
            //Remove selected student from list
            students.Remove(thisStudent);
            //Reduce randomization selector
            selector--;

            //If the size of the group and list size are equal, or there are no students left to sort
            if (groupSize == currentGroupList.Count || students.Count == 0)
            {                 
                groupNumber++;

                studentGroupList.Add(currentGroupList);
                currentGroupList = new List<string>();
                //demonstrates data is being stored properly
                Console.WriteLine(studentGroupList[0][0]);
            }                
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

以下对我有用,如果我的目的正确,我认为不需要selector变量。如果它确实作为students的计数,则还会出现逻辑错误,因为rng.Next(selector)只会取值selector-1,但由于selector是计数减去1,它本身就是一个有效的索引!我还删除了groupNumber变量,因为它与studentGroupList.Count重合。

您只需将此代码插入空项目的Main方法并调试即可查看正在发生的事情。

如果您仍然遇到IndexOutOfRangeException问题,请发布您曾尝试过的代码并遍历studentGroupList,其中完全发生异常。除此之外,我们无能为力,这取决于你的调试技巧:)

  List<string> students = new List<string> { "Peter", "Lisa", "A", "B", "C", "F", "J", "K" };

  Random rng = new Random();

  List<List<string>> studentGroupList = new List<List<string>>();
  List<string> currentGroupList = new List<string>();

  Console.Write("Please enter a number: ");
  int groupSize = int.Parse(Console.ReadLine());

  while (students.Count > 0)
  {
    string thisStudent = students[rng.Next(students.Count)];
    currentGroupList.Add(thisStudent);
    students.Remove(thisStudent);

    if (groupSize == currentGroupList.Count || students.Count == 0)
    {
      groupNumber++;          studentGroupList.Add(currentGroupList);
      currentGroupList = new List<string>();
    }
  }

  // this iterates over all lists in studentGroupList,
  // then over all students in the current list and prints them;
  // it separates the printed groups by empty lines
  foreach (List<string> group in studentGroupList)
  {
    foreach (string student in group)
    {
      Console.WriteLine(student);
    }
    Console.WriteLine();
  }

  Console.ReadKey();