C#:拼接阵列

C#:拼接阵列,c#,arrays,C#,Arrays,假设我有一个名为IDs的long[],数组中有几个元素 在给定索引处拼接/插入新元素的最简单方法是什么 现在我正在做这件事,我认为这不是最好的: long[] IDs = ...; var IDsList = IDs.ToList(); IDsList.Insert(newId, indexToInsertAt); IDs = IDsList.ToArray(); 数组中没有内置任何东西类?!这让我感到非常奇怪,它来自JavaScript世界的[].splice()使用列表而不是数组,因为

假设我有一个名为
IDs
long[]
,数组中有几个元素

在给定索引处拼接/插入新元素的最简单方法是什么

现在我正在做这件事,我认为这不是最好的:

long[] IDs = ...;

var IDsList = IDs.ToList();
IDsList.Insert(newId, indexToInsertAt);

IDs = IDsList.ToArray();
数组中没有内置任何东西
类?!这让我感到非常奇怪,它来自JavaScript世界的
[].splice()

使用
列表而不是数组,因为您需要进行插入操作。

您可以尝试使用

IDs.SetValue(newId, indexToInsertAt);

这似乎有点奇怪,但可能是为了防止开发人员太容易编写性能差的代码而忽略了它。(如果你在中间插入一个新的项目,你可能需要一个可调整的集合,比如清单)。“插入”到一个固定大小的集合中的唯一方法,如<代码>数组< /代码>,是将集合的内容复制到一个新的集合中并将其放置在那里。显然,如果您执行大量插入,这不是最好的主意

如果使用
T[]
数组超出了您的控制范围,并且需要插入,那么自己复制数组至少比您拥有的代码更可取,因为这样可以节省两个昂贵的操作:复制和插入,这需要潜在的多个元素通过一个索引“移位”。(您当前的解决方案将
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;
    }
}
输出:

1 2 3 4 1. 2. 3. 4.
不得不做一些类似的事情,下面是我想到的,类似于Dan Tao的:

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;
T[]newArr=newt[oldArr.Length+1];

//复制数组的第一部分,从newArr[0]开始,你为什么要在列表上使用数组?这让我觉得很奇怪,因为它也来自PHP,但你拥有的是我在C#中使用的,数组类中没有内置任何内容,因为这是列表类的用途,但我与Aaron和Tim Robinson在一起,使用列表并解决问题。这不会插入项目,而是替换项目。如果这就是他想要做的,他会写
IDs[indextoinsert]=newId
。嗯,显然这并不清楚:我必须处理的参数(由接口定义)很长[]。@agilemeansdoaslittlesable:等等,你是OP吗?似乎您有多个标识…嗯,一个接口不能指定字段。除非声明为ref,否则不能调整作为方法参数传递的数组的大小。
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;