這似乎有點奇怪,但很有可能被排除在外,以防止開發人員很容易寫性能差的代碼。 (如果要插入中間一個新的項目,你可能想要一個可調整大小的集合像List<T>
)到「插入」到一個固定大小的採集像Array
的唯一方法是將收集的內容複製到新收集,並把項目那裏。顯然,如果您執行大量插入操作,這不是最好的辦法。
如果使用T[]
陣列的是你的控制之外,並且插入是必要的,複製自己的數組至少是最好你有,因爲它可以節省你昂貴操作的代碼:複製和插入這要求可能有很多元素被一個索引「轉移」。 (您當前的解決方案複製long[]
的內容轉換成一個List<long>
,然後插入一個項目成List<long>
,然後將複製List<long>
回到一個新的long[]
。)
在這種情況下(一個T[]
的選擇是不可協商的),你可以考慮使用擴展方法來完成我剛剛描述的內容。這樣,至少你有一個場景,當你做需要此行爲的可重用的代碼。喜歡的東西:
public static class ArrayHelper
{
public static T[] Insert<T>(this T[] source, int index, T item)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (index < 0 || index > source.Length)
{
throw new ArgumentOutOfRangeException("index");
}
// Allocate a new array with enough space for one more item.
T[] result = new T[source.Length + 1];
// Copy all elements before the insertion point.
for (int i = 0; i < index; ++i)
{
result[i] = source[i];
}
// Insert the new value.
result[index] = item;
// Copy all elements after the insertion point.
for (int i = index; i < source.Length; ++i)
{
result[i + 1] = source[i];
}
return result;
}
}
注意,上面是比你現在是怎樣,因爲它僅需要執行全陣列複製一個時間(不是兩次),相當於有效的多,也沒有按不需要元素的任何中間「移位」。
用法:
int[] numbers = new int[] { 2, 3, 4 };
numbers = numbers.Insert(0, 1);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
輸出:
1
2
3
4
任何理由你正在使用一個列表? –
2010-12-09 20:36:04
它讓我覺得奇怪的是來自PHP,但你有什麼是我在C#中使用的東西# – Dinah 2010-12-09 20:36:40
沒有什麼內置到Array類,因爲這是什麼列表類是 –
2010-12-09 20:37:04