English 中文(简体)
C#: 覆盖阵列
原标题:C#: Splicing array
  • 时间:2010-12-09 20:33:11
  •  标签:
  • c#
  • arrays

请允许我说,我有一份<代码>,长期[],即,其中有若干内容。

在某一指数中pl入/插入新成分的最容易的方法是什么?

现在我这样做了,我认为这样做是最佳的:

long[] IDs = ...;

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

IDs = IDsList.ToArray();

<代码>中没有任何内容。 Array category? 这给我带来非常奇怪的打击,它来自 <世界的<代码>。

问题回答

使用<代码>List<long>,而不是因为你重新需要插入的阵列。

这可能似乎有点奇怪,但可能无法防止开发商撰写业绩不佳的守则。 (如果你在中间插入一个新项目,你可能希望像<代码>List<T>这样的可再生收集。) 将收集的内容复制成new采集,并将项目there<>。 显然,如果你重新做许多插入,这不是最好的想法。

如果使用<代码>T[]阵列不属你控制范围,并且需要插入,那么,复制阵列至少比你所用的代码要好,因为它节省了 2<>>>>>>>>/m>昂贵的操作:复制件和插入,这就要求可能有许多要素被“临时”列入一个指数。 (现有解决办法复制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;




相关问题
Anyone feel like passing it forward?

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. ...

NSArray s, Primitive types and Boxing Oh My!

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 ...

C# Marshal / Pinvoke CBitmap?

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 ...

How to Use Ghostscript DLL to convert PDF to PDF/A

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, ...

Linqy no matchy

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. ...