TK.*_*TK. 3 c# list list-manipulation
如果我有
List<String> text
Run Code Online (Sandbox Code Playgroud)
如何创建特定范围内所有连续元素的子列表,例如
List<String> subList = /* all elements within text bar the first 2*/
Run Code Online (Sandbox Code Playgroud)
还有其他有用的List操作提示和技巧可能有用吗?
Voj*_*vic 12
即使没有LINQ,这也可以工作:
List<String> subList = text.GetRange(2, text.Count - 2);
Run Code Online (Sandbox Code Playgroud)
编辑:修正了一个错字.
subList = text.Skip(2).ToList()
Run Code Online (Sandbox Code Playgroud)
Skip(n)返回包含除前n之外的所有元素的IEnumerable <>.
当你真的需要一个列表之后,ToList()会将其转换回来.