【问题标题】:Creating lists within a list properly在列表中正确创建列表
【发布时间】:2018-06-08 14:57:45
【问题描述】:

我正在尝试创建一个包含从 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)

【问题讨论】:

  • 哪一行有异常?您没有进行任何列表查找(.Query&lt;&gt;() 除外)。
  • 你的意思是像this这样的东西吗?只是很快就写了这篇文章,试图收集你所期望的想法。
  • 您的代码允许 SQL 注入攻击。请修复。
  • 您的错误发生在DriftManager.GetDriftStep(DriftManager 第 26 行)中,该错误未包含在您的问题中。您包含的UpdateListData 根本不涉及错误。

标签: c# list unity3d


【解决方案1】:

您可以根据需要对每个项目执行的操作来尝试类似的操作。

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

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

【讨论】:

    【解决方案2】:

    我在完全理解您要完成的工作时遇到了一点麻烦,但我假设您想要检索主列表中每个列表中的每个对象。

    在处理由其他列表组成的列表时,var lstOfLsts = new List&lt;List&lt;object&gt;&gt;();,我强烈建议使用.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 发挥作用的地方。

    让我知道这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-11
      • 2018-11-01
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      相关资源
      最近更新 更多