当前位置: 首页 > 知识库问答 >
问题:

如何在c中创建列表列表#

鄢晔
2023-03-14

我正在尝试创建列表列表,其中大列表表示纸张包含小列表表示问题的集合,问题列表由问题字符串及其ID组成。在这里我的代码:

public class Genes
{
    public string question { get; set; }
    public int CLO { get; set; }

} 

   List<Genes> questiongene = new List<Genes>();
   List<List<questiongene>> paper = new List<List<questiongene>>();

现在我没有错误地制作问题列表,但是当我尝试创建更大的列表时,Visual Studio无法将可变问题类型识别为类型,哪里错了?

共有3个答案

左丘照
2023-03-14
List<List<string>> myList = new List<List<string>>();
myList.Add(new List<string> { "a", "b" });
myList.Add(new List<string> { "c", "d", "e" });
myList.Add(new List<string> { "qwerty", "asdf", "zxcv" });
myList.Add(new List<string> { "a", "b" });

// To iterate over it.
foreach (List<string> subList in myList)
{
    foreach (string item in subList)
    {
        Console.WriteLine(item);
    }
}
林博厚
2023-03-14

您的代码中没有questiongene类型,只有Genes类型。创建列表:

List<List<Genes>> paper = new List<List<Genes>>();

或创建一个具有列表问题的新类型论文:

public class Paper
{
    public List<Genes> questionGenes { get; set; }

    // default constructor
    public Paper()
    {
         questionGenes = new List<Genes>();
    }

    // constructor that creates a paper from a List<Genes>
    public Paper(List<Genes> questionGene)
    {
         questionGenes = questionGene;
    }

}

然后创建一个纸列表:

List<Paper> papers = new List<Paper>();
papers.Add(new Paper(questiongene));
谭健柏
2023-03-14

我会这样做,而不是一个基本的清单

//This is your class for a single question.  A list of these will 
//make up a paper.
public class Genes
{
    public string Question{ get; set; }
    public int CLO { get; set; }
}

//This is a Paper, which contains a list of Questions.
public class Paper
{
    public List<Genes> Questions { get; set; }
}
..
..
//To use:  First create a list of papers - 
//this is a stack of papers.
List<Paper> stackOfPapers = new List<Paper>();

//now create a list of questions which we can add to a paper
List<Genes> questions = new List<Genes>();

//Now we need to create a question to add to the list of questions.
Genes newQuestion = new Genes();    
newQuestion.Question = "How many roads must a man walk down?";
newQuestion.CLO = 42;

//now add the question to the list.
questions.Add(newQuestion);

//now we need to create a paper.  This will represent one
//paper in our stack of papers.
Paper newPaper = new Paper();
//add our list of Questions to the paper.
newPaper.Questions = questions;

//and finally, add the paper to the stack of papers.
stackOfPapers.Add(newPaper);

//or alternatively, you can use the object initializer syntax to 
//do this all in one.  Note I've also added more than one paper to 
//the list of papers, and each paper has two questions contained in it:    
var newStackOfPapers = 
    new List<Paper>
    {
        new Paper
        {
            Questions = new List<Genes> {
                new Genes
                {
                    Question = "How many roads must a man walk down?",
                    CLO = 42
                },
                new Genes
                {
                    Question = "Another Question?",
                    CLO = 111
                }
            }
        },
        //add Another paper...
        new Paper
        {
            Questions = new List<Genes> {
                new Genes
                {
                    Question = "This is the first question on the second paper?",
                    CLO = 22
                },
                new Genes
                {
                    Question = "Another Question?",
                    CLO = 33
                }
            }
        },

    };
 类似资料:
  • 对于C#中泛型列表的泛型列表的概念,我似乎有点难以理解。我认为问题源于

  • 我不熟悉java和springboot。我正在尝试使用springboot创建一个CRUD应用程序。我使用MySQL存储数据。 员工模式- 员工资源库- 员工控制员- 上面的控制器在JSON对象数组表单中给出了结果,如下所示 但我需要以下表格的回复 非常感谢你的帮助。

  • 我已经使用Hibernate将Java对象映射到PostgreSQL数据库。UserDetails类是用来添加用户的实体类,它包含一个名为Address的嵌入对象。 userdetails.java 生成的SQL查询如下所示,它显示列的添加顺序与声明列的顺序不同。 在Hibernate中添加哪些列有什么规则吗?

  • 我想列个这样的单子。请帮帮我.谢了。

  • 问题内容: 如何在Django(Python)中像Google App Engine(Python)中的ListProperty属性一样创建ListField ?我的数据是这样的名单:3,4,5,6,7,8。 我必须定义什么属性,以及如何从中获取值? 问题答案: 使用你可以使用的类型来重新研究它。但这有一些假设,例如你不在列表中存储复杂类型的事实。出于这个原因,我曾经强制只将简单的内置类型作为成员

  • 我有一个名为MarkerCustom的类文件。MarkerCustom有一个构造函数,它接受三个不同类型的变量。 从我的main Main活动中,我希望加载一个GLSurface视图,该视图将获取MarkerCustom的每个实例的ArrayList,我希望将这些实例与将传递给MarkerCustom的构造函数的每个实例的数据一起加载到GLSurface中。 让我们调用数组列表; 我需要看起来像这