请允许我说,我有一份<代码>,长期[],即
在某一指数中pl入/插入新成分的最容易的方法是什么?
现在我这样做了,我认为这样做是最佳的:
long[] IDs = ...;
var IDsList = IDs.ToList();
IDsList.Insert(newId, indexToInsertAt);
IDs = IDsList.ToArray();
<代码>中没有任何内容。 Array category? 这给我带来非常奇怪的打击,它来自 <世界的<代码>。
请允许我说,我有一份<代码>,长期[],即
在某一指数中pl入/插入新成分的最容易的方法是什么?
现在我这样做了,我认为这样做是最佳的:
long[] IDs = ...;
var IDsList = IDs.ToList();
IDsList.Insert(newId, indexToInsertAt);
IDs = IDsList.ToArray();
<代码>中没有任何内容。 Array category? 这给我带来非常奇怪的打击,它来自 <世界的<代码>。
使用<代码>List<long>,而不是因为你重新需要插入的阵列。
这可能似乎有点奇怪,但可能无法防止开发商撰写业绩不佳的守则。 (如果你在中间插入一个新项目,你可能希望像<代码>List<T>这样的可再生收集。) 将收集的内容复制成
如果使用<代码>T[]阵列不属你控制范围,并且需要插入,那么,复制阵列至少比你所用的代码要好,因为它节省了long [
into a List<long>
,后插入以下条目:List<long>
,then <>>>m> /em> 份List<long>
>>mback<>/em> into a new 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;
}
}
通知说,上述措施比你现在的效率要大得多,因为它只需要履行相当于全部文件one<>>>>/em>(不是两次)的时间,而且也不需要任何中间的“变换”内容。
使用:
int[] numbers = new int[] { 2, 3, 4 };
numbers = numbers.Insert(0, 1);
foreach (int number in numbers)
{
Console.WriteLine(number);
}
产出:
1 2 3 4
做一些类似的事情,这里是我即将到来的。
T[] newArr = new T[oldArr.Length+1];
//copy first part of the array, starting with newArr[0] <- oldArr[0], up to the insertion point
System.Array.Copy(oldArr, 0, newArr, 0, insertIndex, insertIndex);
//insert new element
newArr[insertIndex] = spliceElem;
//copy the rest of the array, from newArr[insert+1] <- oldArr[insert] to the end
System.Array.Copy(oldArr, insertIndex, newArr, insertIndex + 1, oldArr.Length-insertIndex);
return newArr;
你可以尝试
IDs.SetValue(newId, indexToInsertAt);
http://msdn.microsoft.com/en-us/library/k3bwkb0.aspx”rel=“nofollow”> 页: 1
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...