回答
如果你不想使用List:
var foos = new List<Foo>(array);
foos.RemoveAt(index);
return foos.ToArray();
你可以试试,我没有实际测试过这种扩展方法:
public static T[] RemoveAt<T>(this T[] source, int index)
{
T[] dest = new T[source.Length - 1];
if(index > 0)
Array.Copy(source, 0, dest, 0, index);
if(index < source.Length - 1)
Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
return dest;
}
而且使用它像:
Foo[] bar = GetFoos();
bar = bar.RemoveAt(2);
这里是我是如何做到的?
public static ElementDefinitionImpl[] RemoveElementDefAt(
ElementDefinition[] oldList,
int removeIndex
)
{
ElementDefinitionImpl[] newElementDefList = new ElementDefinitionImpl[ oldList.Length - 1 ];
int offset = 0;
for (int index = 0; index < oldList.Length; index++)
{
ElementDefinitionImpl elementDef = oldList[ index ] as ElementDefinitionImpl;
if (index == removeIndex)
{
// This is the one we want to remove, so we won't copy it. But
// every subsequent elementDef will by shifted down by one.
offset = -1;
}
else
{
newElementDefList[ index + offset ] = elementDef;
}
}
return newElementDefList;
}
阵列的本质是它们的长度是不变的。您不能添加或删除任何数组项目。
您将不得不创建一个短一个元素的新数组,并将旧项目复制到新数组中,但不包括要删除的元素。
因此,最好使用List而不是数组。
阵列转换到列表`列表
在普通数组中,您必须将所有大于2的数组输入进行混洗,然后使用Resize方法调整它的大小。使用ArrayList可能会更好。
这是一个旧版本,它适用于.NET框架的1.0版本,并且不需要generi c类型。
public static Array RemoveAt(Array source, int index)
{
if (source == null)
throw new ArgumentNullException("source");
if (0 > index || index >= source.Length)
throw new ArgumentOutOfRangeException("index", index, "index is outside the bounds of source array");
Array dest = Array.CreateInstance(source.GetType().GetElementType(), source.Length - 1);
Array.Copy(source, 0, dest, 0, index);
Array.Copy(source, index + 1, dest, index, source.Length - index - 1);
return dest;
}
这用于这样的:
class Program
{
static void Main(string[] args)
{
string[] x = new string[20];
for (int i = 0; i < x.Length; i++)
x[i] = (i+1).ToString();
string[] y = (string[])MyArrayFunctions.RemoveAt(x, 3);
for (int i = 0; i < y.Length; i++)
Console.WriteLine(y[i]);
}
}
我使用用于从一个对象数组移除元素此方法。在我的情况下,我的阵列长度很小。所以如果你有大阵列,你可能需要另一种解决方案。
private int[] RemoveIndices(int[] IndicesArray, int RemoveAt)
{
int[] newIndicesArray = new int[IndicesArray.Length - 1];
int i = 0;
int j = 0;
while (i < IndicesArray.Length)
{
if (i != RemoveAt)
{
newIndicesArray[j] = IndicesArray[i];
j++;
}
i++;
}
return newIndicesArray;
}
不完全相同的方式去了解这一点,但如果情况是琐碎和你珍惜你的时间,你可以尝试一下本作空类型。
Foos[index] = null
和更高版本检查逻辑中的空条目..
第一步
您需要将数组转换成列表,你可以这样写
// Convert An array of string to a list of string
public static List<string> ConnvertArrayToList(this string [] array) {
// DECLARE a list of string and add all element of the array into it
List<string> myList = new List<string>();
foreach(string s in array){
myList.Add(s);
}
return myList;
}
第二步
扩展方法写一个扩展方法来转换回列表到一个数组
// convert a list of string to an array
public static string[] ConvertListToArray(this List<string> list) {
string[] array = new string[list.Capacity];
array = list.Select(i => i.ToString()).ToArray();
return array;
}
最后的步骤
写下您的最后一个方法,但要记住转换回像代码阵列之前删除索引的元素显示
public static string[] removeAt(string[] array, int index) {
List<string> myList = array.ConnvertArrayToList();
myList.RemoveAt(index);
return myList.ConvertListToArray();
}
例子代码可以在my blog被发现,保持跟踪。
这是删除一个数组元素,如净3.5的一种方式,而不会复制到另一个阵列 - 使用相同的阵列实例与Array.Resize<T>
:
像往常一样,我迟到了派对......
我想添加另一个选项到已经存在的很好的解决方案列表中。 =)
我认为这是扩展的好机会。
参考: http://msdn.microsoft.com/en-us/library/bb311042.aspx
所以,我们定义了一些静态类,并在其中,我们的方法。
之后,我们可以继续使用我们的扩展方法。 =)
using System;
namespace FunctionTesting {
// The class doesn't matter, as long as it's static
public static class SomeRandomClassWhoseNameDoesntMatter {
// Here's the actual method that extends arrays
public static T[] RemoveAt<T>(this T[] oArray, int idx) {
T[] nArray = new T[oArray.Length - 1];
for(int i = 0; i < nArray.Length; ++i) {
nArray[i] = (i < idx) ? oArray[i] : oArray[i + 1];
}
return nArray;
}
}
// Sample usage...
class Program {
static void Main(string[] args) {
string[] myStrArray = { "Zero", "One", "Two", "Three" };
Console.WriteLine(String.Join(" ", myStrArray));
myStrArray = myStrArray.RemoveAt(2);
Console.WriteLine(String.Join(" ", myStrArray));
/* Output
* "Zero One Two Three"
* "Zero One Three"
*/
int[] myIntArray = { 0, 1, 2, 3 };
Console.WriteLine(String.Join(" ", myIntArray));
myIntArray = myIntArray.RemoveAt(2);
Console.WriteLine(String.Join(" ", myIntArray));
/* Output
* "0 1 2 3"
* "0 1 3"
*/
}
}
}
LINQ单行溶液:
myArray = myArray.Where((source, index) => index != 1).ToArray();
在实施例中的1
是元素的索引,以除去 - 在这个例子中,每原来的问题,所述第二元件(其中1
是C#中基于零的数组索引的第二个元素)。
一个更完整的例子:
string[] myArray = { "a", "b", "c", "d", "e" };
int indexToRemove = 1;
myArray = myArray.Where((source, index) => index != indexToRemove).ToArray();
运行的片段后,myArray
值将是{ "a", "c", "d", "e" }
。
private int[] removeFromArray(int[] array, int id)
{
int difference = 0, currentValue=0;
//get new Array length
for (int i=0; i<array.Length; i++)
{
if (array[i]==id)
{
difference += 1;
}
}
//create new array
int[] newArray = new int[array.Length-difference];
for (int i = 0; i < array.Length; i++)
{
if (array[i] != id)
{
newArray[currentValue] = array[i];
currentValue += 1;
}
}
return newArray;
}
下面是我根据一些现有答案生成的一小组帮助方法。它利用两个扩展和静态方法与最大idealness参考参数:
public static class Arr
{
public static int IndexOf<TElement>(this TElement[] Source, TElement Element)
{
for (var i = 0; i < Source.Length; i++)
{
if (Source[i].Equals(Element))
return i;
}
return -1;
}
public static TElement[] Add<TElement>(ref TElement[] Source, params TElement[] Elements)
{
var OldLength = Source.Length;
Array.Resize(ref Source, OldLength + Elements.Length);
for (int j = 0, Count = Elements.Length; j < Count; j++)
Source[OldLength + j] = Elements[j];
return Source;
}
public static TElement[] New<TElement>(params TElement[] Elements)
{
return Elements ?? new TElement[0];
}
public static void Remove<TElement>(ref TElement[] Source, params TElement[] Elements)
{
foreach (var i in Elements)
RemoveAt(ref Source, Source.IndexOf(i));
}
public static void RemoveAt<TElement>(ref TElement[] Source, int Index)
{
var Result = new TElement[Source.Length - 1];
if (Index > 0)
Array.Copy(Source, 0, Result, 0, Index);
if (Index < Source.Length - 1)
Array.Copy(Source, Index + 1, Result, Index, Source.Length - Index - 1);
Source = Result;
}
}
在性能方面,它是体面的,但它很可能得到改善。Remove
依靠IndexOf
和要通过调用RemoveAt
删除的每个元素创建一个新的阵列。
IndexOf
是因为它并不需要返回原始数组的唯一扩展方法。 New
接受某种类型的多个元素来产生一个新的这种类型的数组。所有其他方法必须接受原始数组作为参考,所以不需要在后面分配结果,因为这已经在内部发生了。
我已经定义了一个Merge
方法用于合并两个阵列;然而,通过传递一个实际的数组与多个单独的元素,已经可以用Add
方法完成。因此,Add
可以通过以下两种方式来连接两个组元素:
Arr.Add<string>(ref myArray, "A", "B", "C");
或者
Arr.Add<string>(ref myArray, anotherArray);
- 1. 删除数组的元素
- 2. JavaScript删除元素数组
- 3. 删除空数组元素
- 4. 删除数组元素(C#)
- 5. 删除JS数组元素
- 6. 删除[]数组元素
- 7. 删除数组元素
- 8. Pyschools删除常见元素
- 9. 删除数组中的空值元素
- 10. 通过underscore.js删除数组的元素
- 11. 存储,删除数组中的元素
- 12. 删除数组中一行的元素
- 13. 如何删除数组中的元素
- 14. Java的字符数组 - 删除元素
- 15. 删除数组中无用的元素
- 16. 删除数组中的元素
- 17. 如何删除数组中的元素?
- 18. 删除给定数组中的元素
- 19. 删除多维数组的父元素?
- 20. 删除jquery中的数组元素
- 21. 如何删除常见的元素在PHP数组
- 22. 使用PullFilter删除数组元素时出现异常
- 23. 删除数组元素时出错
- 24. 删除动态数组元素
- 25. 从数组中删除一些元素
- 26. Rails Ransack - 从数组中删除元素
- 27. 从mongo数组中删除null元素
- 28. 删除元素来排序数组
- 29. 按索引删除数组元素
- 30. 删除数组元素完全
使用`System.Collections.ObjectModel.Collection`。 –
abatishchev
2009-01-19 12:46:27
对于我的游戏,我使用了“null at index”数据结构。基本上,内部数组(缓冲区)是静态大小,而不是删除索引和调整数组大小,我只是使索引为空。当我需要添加一个项目时,我只找到第一个非空索引并将其放在那里。工作得很好,但显然不适合所有事情。 – Krythic 2016-09-17 15:00:59