C#:拼接数组 [英] C#: Splicing array

查看:175
本文介绍了C#:拼接数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有一个 long [] 名为 ID ,其中包含一些元素。

Let's say I have a long[] called IDs with a few elements in the array.

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

What's the easiest way to splice into/insert a new element at a given index?

现在我'正在这样做,我认为这不是最佳选择:

Right now I'm doing this, and I don't think it's optimal:

long[] IDs = ...;

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

IDs = IDsList.ToArray();

Array 没有内置函数类?!来自JavaScript世界的 []。splice()

There's nothing built-in into the Array class?! This strikes me as very odd, coming from the JavaScript world of [].splice().

推荐答案

使用 List< long> 代替数组,因为需要插入。

Use a List<long> instead of the array since you're needing to make inserts.

这篇关于C#:拼接数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆