C# 将嵌套列表与逻辑相结合

C# 将嵌套列表与逻辑相结合,c#,list,nested,wrapper,C#,List,Nested,Wrapper,我使用的游戏引擎无法序列化嵌套列表,例如List。我需要的是一个将多个列表存储到一个列表中的快速解决方案。我将自己写这篇文章,但我想知道是否已经存在任何解决方案 是否有包装器可以将“虚拟”嵌套列表存储到一个大列表中,同时提供您希望从单独列表中获得的功能?您可以使用它来展平嵌套列表: List<int> flattened = allLists.SelectMany(l => l).ToList(); 您能否澄清您是否在追求: 可以表示嵌套列表的序列化库(例如,JSON.NET

我使用的游戏引擎无法序列化嵌套列表,例如
List
。我需要的是一个将多个列表存储到一个列表中的快速解决方案。我将自己写这篇文章,但我想知道是否已经存在任何解决方案

是否有包装器可以将“虚拟”嵌套列表存储到一个大列表中,同时提供您希望从单独列表中获得的功能?

您可以使用它来展平嵌套列表:

List<int> flattened = allLists.SelectMany(l => l).ToList();

您能否澄清您是否在追求:

  • 可以表示嵌套列表的序列化库(例如,JSON.NET应该能够)
  • 一种使列表扁平化的方法

  • 像这样的怎么样:

    要展平列表,请使用其他人建议的方法制作一个元组展平列表(注意,下面的所有代码都未经测试):

    List myStartingList=newlist();
    List myFlatList=新列表();
    对于(var iOuter=0;iOuter
    为了让你放松:

    List<List<int>> myNestedList = new List<List<int>>();
    int iOuter=-1;
    foreach (var t in myFlattenedList)
    {
        if (iOuter != t.Item1)
            myNestedList.Add(new List<Int>());
        iOuter = t.Item1;
        myNestedList[t.Item1][t.Item2] = t.Item3;
    }
    
    List myNestedList=new List();
    int iOuter=-1;
    foreach(MyFlattedList中的var t)
    {
    如果(iOuter!=t.Item1)
    添加(新列表());
    iOuter=t.Item1;
    myNestedList[t.Item1][t.Item2]=t.Item3;
    }
    
    如果它不能序列化嵌套列表,这表明它使用的是非标准序列化程序……那么,假设是这样,它可以序列化什么?它可以序列化标准(非嵌套)列表。有什么原因需要使用该序列化程序,而不是标准序列化程序吗?我相信游戏引擎(Unity3D)必须使用自己的序列化程序。是否可以将展开的列表重新展开为嵌套列表?一种展开和展开嵌套列表的方法。
    List<List<int>> myStartingList = new List<List<int>>();
    List<Tuple<int, int, int>> myFlatList = new List<Tuple<int, int, int>>();
    for (var iOuter = 0; iOuter < myStartingList.Count; iOuter++)
        for (var iInner = 0; iInner < myStartingList[iOuter].Count; iInner++)
            myFlatList.Add(new Tuple<int, int, int>(iOuter, iInner, myStartingList[iOuter][iInner]);
    
    List<List<int>> myNestedList = new List<List<int>>();
    int iOuter=-1;
    foreach (var t in myFlattenedList)
    {
        if (iOuter != t.Item1)
            myNestedList.Add(new List<Int>());
        iOuter = t.Item1;
        myNestedList[t.Item1][t.Item2] = t.Item3;
    }