获取嵌套列表中的值索引
问题描述:
我在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]
第二[0]访问嵌套列表的索引。由于list[0]
给出列表的第一个元素,因此列表[0] [0]给出列表的第一个元素的第一个元素。
答
你的猜测几乎是正确的。你想使用什么是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
其原因在于List<string>
内没有物品通过nestedList[0]
返回:
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!");
}
当枚举的元素是class
ES,则该方法返回任一可枚举或null
的第一个元素。
答
您可以使用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);
}
Here是一个工作的dotnet小提琴。
答
如果你只是需要访问特定的指数,那么你的方法是:
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
}
这里有个提示:'列表 firstNestedList = nestedList [0];' - 你如何访问列表的第一项? –
cubrr
你不应该像'nestedList [0 [0]]一样访问''。你应该这样做'nestedList [0] [0]' –