C#中数组的子集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1792470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subset of Array in C#
提问by LJM
If I have an array with 12 elements and I want a new array with that drops the first and 12th elements. For example, if my array looks like this:
如果我有一个包含 12 个元素的数组,并且我想要一个包含第一个和第 12 个元素的新数组。例如,如果我的数组如下所示:
__ __ __ __ __ __ __ __ __ __ __ __
a b c d e f g h i j k l
__ __ __ __ __ __ __ __ __ __ __ __
I want to either transform it or create a new array that looks like
我想转换它或创建一个看起来像的新数组
__ __ __ __ __ __ __ __ __ __
b c d e f g h i j k
__ __ __ __ __ __ __ __ __ __
I know I can do it by iterating over them. I was just wondering if there was a cleaner way built into C#.
我知道我可以通过迭代它们来做到这一点。我只是想知道 C# 中是否有更简洁的方法。
**UPDATED TO FIX A TYPO. Changed 10 elements to 12 elements.
** 更新以修正一个错字。将 10 个元素更改为 12 个元素。
采纳答案by Noldorin
LINQ is your friend. :)
LINQ 是你的朋友。:)
var newArray = oldArray.Skip(1).Take(oldArray.Length - 2).ToArray();
Somewhat less efficient than manually creating the array and iterating over it of course, but far simple...
当然比手动创建数组并迭代它效率低一些,但要简单得多......
The slightly lengithier method that uses Array.Copy
is the following.
使用的稍微长一点的方法Array.Copy
如下。
var newArray = new int[oldArray.Count - 2];
Array.Copy(oldArray, 1, newArray, 0, newArray.Length);
回答by Bob
You can do this with Array.Copy
or LINQ.
您可以使用Array.Copy
或 LINQ执行此操作。
var letters = string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i" };
int length = letters.Length - 2;
var items = new string[length];
Array.Copy(letters, 1, items, 0, length);
// or
var items = letters.Skip(1).Take(length).ToArray();
回答by Jason Punyon
string[] s = initialize the array...
var subset = s.Skip(1).Take(s.Length - 2).ToArray();
回答by Dan Bystr?m
Array.Copy() will do that for you, but you still have to create your new array with its correct size.
Array.Copy() 将为您完成此操作,但您仍然必须创建具有正确大小的新数组。
回答by Dr. Wily's Apprentice
Linq is all nice and snazzy, but if you're looking for a 1-liner you could just throw together your own utility functions:
Linq 既漂亮又时髦,但如果您正在寻找 1-liner,您可以将自己的实用程序函数放在一起:
static class ArrayUtilities
{
// create a subset from a range of indices
public static T[] RangeSubset<T>(this T[] array, int startIndex, int length)
{
T[] subset = new T[length];
Array.Copy(array, startIndex, subset, 0, length);
return subset;
}
// create a subset from a specific list of indices
public static T[] Subset<T>(this T[] array, params int[] indices)
{
T[] subset = new T[indices.Length];
for (int i = 0; i < indices.Length; i++)
{
subset[i] = array[indices[i]];
}
return subset;
}
}
So then you could do the following:
那么你可以执行以下操作:
char[] original = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
// array containing 'b' - 'f'
char[] rangeSubset = original.RangeSubset(1, original.Length - 2);
// array containing 'c', 'd', and 'f'
char[] specificSubset = original.Subset(2, 3, 5);
回答by Tobias
If you want to avoid manually indexing the array. Don't try to pull request that anywhere though:
如果你想避免手动索引数组。不要尝试在任何地方拉取请求:
var newArray = oldArray.Skip(1).Reverse().Skip(1).Reverse().ToArray()
回答by orad
You can use ArraySegment<T>
structure like below:
您可以使用ArraySegment<T>
如下结构:
var arr = new[] { 1, 2, 3, 4, 5 };
var offset = 1;
var count = 2;
var subset = new ArraySegment<int>(arr, offset, count)
.ToArray(); // output: { 2, 3 }
Check here for an extension methodthat makes use of it even easier.
在此处查看可以更轻松地使用它的扩展方法。
回答by Prasanth Louis
C# 8 has a Range
and Index
type
C# 8 有一个Range
andIndex
类型
char[] a = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l' };
Index i1 = 1; // number 1 from beginning
Index i2 = ^1; // number 1 from end
var slice = a[i1..i2]; // { 'b','c','d','e','f','g','h','i','j' }