如何获得在C#中的子列表 [英] How to Get a Sublist in C#

查看:198
本文介绍了如何获得在C#中的子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表与LT;弦乐> 和我需要的子列表出这个名单。是否有可用的名单中的任何方法在.NET 3.5?

I have a List<String> and i need to take a sublist out of this list. Is there any methods of List available for this in .NET 3.5?

推荐答案

您想列表:: GetRange(firstIndex和计数) 。请参见 http://msdn.microsoft.com/en-us/library/21k0e39c.aspx

You want List::GetRange(firstIndex, count). See http://msdn.microsoft.com/en-us/library/21k0e39c.aspx

// I have a List called list
List sublist = list.GetRange(5, 5); // (gets elements 5,6,7,8,9)
List anotherSublist = list.GetRange(0, 4); // gets elements 0,1,2,3)

是你后?

如果你正在寻找从原来的列表中删除子列表中的项目,你可以再做:

If you're looking to delete the sublist items from the original list, you can then do:

// list is our original list
// sublist is our (newly created) sublist built from GetRange()
foreach (Type t in sublist)
{
    list.Remove(t);
}

这篇关于如何获得在C#中的子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆