正确创建列表中的列表

时间:2018-06-08 14:57:46

标签: c# list unity3d

我正在尝试创建一个List,其中包含从SQLite表生成的列表集合,但我无法让事情发挥作用。我尝试过的当前迭代是这样的。使用列表集合填充主列表然后在列表中循环的正确方法是什么?

// Single Drift list
public static List<Drift> Drift = new List<Drift>();

// List of all available drifts
public static List<List<Drift>> Drifts = new List<List<Drift>>();


public void UpdateListData(string driftID)
    {
        string sql = "SELECT * FROM \"" + driftID + "\" ";
        GameManager.Drifts.Add(dbManager.Query<Drift>(sql));

        foreach (List<Drift> drift in GameManager.Drifts)
        {
            Debug.Log("is in the Drifts list");
        }
    }

但它给了我错误:

ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
System.Collections.Generic.List`1[Drift].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633)
DriftManager.GetDriftStep (Int32 currentStep) (at Assets/_Scripts/DriftManager.cs:26)
DriftManager.DisplayDriftStep (Int32 step) (at Assets/_Scripts/DriftManager.cs:98)
EventManager.GetDriftStep (Int32 step) (at Assets/_Scripts/EventManager.cs:43)
DriftManager.CheckDriftProgress () (at Assets/_Scripts/DriftManager.cs:77)
DriftManager.InitDrift (Int32 driftNumSteps, System.String driftName) (at Assets/_Scripts/DriftManager.cs:67)
EventManager.GenerateNewDrift (Int32 steps, System.String title) (at Assets/_Scripts/EventManager.cs:14)
GameManager.<Start>m__0 () (at Assets/_Scripts/GameManager.cs:61)
UnityEngine.Events.InvokableCall.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)

2 个答案:

答案 0 :(得分:2)

您可以尝试这样的操作,具体取决于您对每件物品的操作。

foreach (var drift in GameManager.Drifts)
{
    foreach(var item in drift)
    {
        //do something with the item
    }    
}

这应该使用嵌套列表正确迭代该Drifts集合中的每个项目。

答案 1 :(得分:2)

我在完全理解你想要完成的事情时遇到一些麻烦,但我想你想要检索主列表中每个列表中的每个对象。

在处理由其他列表var lstOfLsts = new List<List<object>>();组成的列表时,我强烈建议您使用.SelectMany

我创建了example

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        var lstIntOne = new List<int>() {1,2,3};
        var lstIntTwo = new List<int>() {4,5,6};
        var lstIntThree = new List<int>() {7,8,9};

        var lstOfLstInts = new List<List<int>>();

        lstOfLstInts.Add(lstIntOne);
        lstOfLstInts.Add(lstIntTwo);
        lstOfLstInts.Add(lstIntThree);

        var lstAllNumbers = lstOfLstInts.SelectMany(x => x).ToList();

        foreach(var item in lstAllNumbers)
        {
            Console.WriteLine(item);    
        }
    }
}

// Output
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9

所以,我有3个数字列表,我想把每个列表放在一个主列表中。然后我想遍历大列表,并检索每个号码,这是.SelectMany发挥作用的地方。

让我知道这有帮助。