获取嵌套列表中值的索引

本文关键字:索引 嵌套 列表 获取 | 更新日期: 2023-09-27 18:26:16

我在C#中使用嵌套列表时遇到了一些问题。

List<List<string>> nestedList = new List<List<string>>();
nestedList.Add(new List<string> { "test1" } );
nestedList.Add(new List<string> { "test2" } );
nestedList.Add(new List<string> { "test3" } );

因此,如果我是正确的,nestedList现在有3个不同的列表,每个列表都有一个值。

我想做的是

if (nestedList[0[0]]) == "test1")

(如果第一个列表的第一个值等于"test1"

如果列表的特定索引包含"test1",我该怎么办?

获取嵌套列表中值的索引

你的猜测几乎是正确的。您要使用的是nestedList[0][0]:

List<List<string>> nestedList = new List<List<string>>();
nestedList.Add(new List<string> { "test1" } );
nestedList.Add(new List<string> { "test2" } );
nestedList.Add(new List<string> { "test3" } );
if (nestedList[0][0] == "test1")
{
    Console.WriteLine("Test 1!");
}

如果它能帮助你理解语法,这里有一段等效的代码:

List<List<string>> nestedList = new List<List<string>>();
nestedList.Add(new List<string> { "test1" } );
nestedList.Add(new List<string> { "test2" } );
nestedList.Add(new List<string> { "test3" } );
List<string> firstList = nestedList[0]; // Here's your new List<string> { "test1" }
if (firstList[0] == "test1")
{
    Console.WriteLine("Test 1!");
}

但是,如果您不能完全确定所有列表都已填充,那么访问子列表时需要小心。例如,以下示例将用ArgumentOutOfRangeException向您致意,因为nestedList[0]返回的List<string>中没有项目:

List<List<string>> nestedList = new List<List<string>>();
nestedList.Add(new List<string>());
nestedList.Add(new List<string>());
nestedList.Add(new List<string>());
if (nestedList[0][0] == "test1") // Index was out of range. Must be non-negative and less than the size of the collection.Parameter name: index
{
    Console.WriteLine("Test 1!");
}

例如,您可以通过首先检查父列表项计数来确保:

if (nestedList[0].Count> 0 && nestedList[0][0] == "test1")
{
    Console.WriteLine("Test 1!");
}

如果您想要一种安全的方式来访问实现IEnumerable<T>接口的任何东西的第一个元素(基本上是框架中每个集合类的),您可以使用LINQ(添加using System.Linq;FirstOrDefault方法:

if (nestedList[0].FirstOrDefault() == "test1")
{
    Console.WriteLine("Test 1!");
}

当可枚举的元素是classes时,该方法返回可枚举的第一个元素或null

这样做:

nestedList[0][0]

第二个[0]访问嵌套列表的索引。由于list[0]给出了列表的第一个元素,所以list[0][0]给出了列表第一个元素的第一个元件。

您可以使用LINQ查询嵌套列表以获取项目,如果您获得了该项目,则使用IndexOf方法来获取索引

List<List<string>> nestedList = new List<List<string>>();
nestedList.Add(new List<string> { "test1" } );
nestedList.Add(new List<string> { "test2" } );
nestedList.Add(new List<string> { "test3" } );
var tocheck="test3";
var item = nestedList.Where(s=>s.Any(d=>d==tocheck)).FirstOrDefault();
if(item!=null)
{
   var itemIndex=nestedList.IndexOf(item);
  // Console.WriteLine(itemIndex);
}

这是一个正在工作的网络小提琴。

如果您只需要访问特定的索引,那么您的方法是:

nestedList[0][0]; //("test1")
nestedList[1][0]; //("test2") ...

但是,如果你需要找到哪个索引包含该字符串,你应该使用这样的方法:

  public int Test(string value)
    {
        List<List<string>> nestedList = new List<List<string>>();
        nestedList.Add(new List<string> { "test1" });
        nestedList.Add(new List<string> { "test2" });
        nestedList.Add(new List<string> { "test3" });
        for (int i = 0; i < nestedList.Count; i++)
        {
            if (nestedList[i].Any(m => m == value))
                return i;
        }
        //not found
        return -1;
    }
    //To use:
    public void Program() 
    {
        Console.WriteLine("found in index: {0}", Test("test3")); //found in index: 2 
        Console.WriteLine("found in index: {0}", Test("test4")); //found in index: -1
    }