參考來源: https://www.dotnetperls.com/array-slice
用過其他能矩陣式運算的程式語言都對Slice不陌生,因此一個方便的好方法就應該得到廣傳
public static class Extensions
{
/// <summary>
/// Get the array slice between the two indexes.
/// ... Inclusive for start index, exclusive for end index.
/// </summary>
public static T[] Slice<T>(this T[] source, int start, int end)
{
// Handles negative ends.
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
// Return new array.
T[] res = new T[len];
for (int i = 0; i < len; i++)
{
res[i] = source[i + start];
}
return res;
}
}
文章標籤
全站熱搜
