簡體   English   中英

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

[英]how to create list of lists in c#

我正在嘗試創建列表列表,其中大列表代表論文包含小列表代表問題的集合,問題列表由問題字符串及其 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 無法將變量問題類型識別為一種類型,錯在哪里?

我會這樣做,而不是像建議的那樣使用基本的List<List<Genes>> ,因為這樣就很清楚每個項目是什么......

//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
                }
            }
        },

    };

您的代碼中沒有類型問題基因,只有類型的基因。 創建列表列表:

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;
    }

}

然后創建一個 Paper 列表:

List<Paper> papers = new List<Paper>();
papers.Add(new Paper(questiongene));
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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM