的LINQ是所有漂亮和時髦的,但如果你正在尋找一個1班輪你可以只編寫自己的效用函數:
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;
}
}
,那麼你可以做到以下幾點:
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);
雖然不是「j」數組中的第10個元素嗎?或者你的意思是第一個和最後一個元素? – 2009-11-24 20:07:58