Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从数组中删除一个元素并将其余元素移回?(C#)_C# - Fatal编程技术网

如何从数组中删除一个元素并将其余元素移回?(C#)

如何从数组中删除一个元素并将其余元素移回?(C#),c#,C#,下面是一个数组示例 int[] N = new int[]{1,0,6,0,3,4}; for (int i = 0; i < N.Length; i++){ if (N[i] == 0){ //remove N[i] and moveback everything } foreach (string i in N) { Console.Write("{0} ", i + " "); } 筛选以创建新阵列 N = N.Where(x

下面是一个数组示例

int[] N = new int[]{1,0,6,0,3,4};
for (int i = 0; i < N.Length; i++){
    if (N[i] == 0){
    //remove N[i] and moveback everything }
        foreach (string i in N) {
            Console.Write("{0} ", i + " ");
}

筛选以创建新阵列

N = N.Where(x => x != 0).ToArray();

筛选以创建新阵列

N = N.Where(x => x != 0).ToArray();

它似乎非常适合一般的扩展方法,并且有一个很好的快速解决方案

注意:这将重新创建一个数组

给定的

public static class Extensions
{
   public static T[] RemoveElement<T>(this T[] source, int index)
      where T : new()
   {

      if(index >= source.Length) throw new ArgumentOutOfRangeException(nameof(index));

      // create new array
      var result = new T[source.Length - 1];
      // Copy the first part
      Array.Copy(source, 0, result, 0, index);
      // Copy the second part
      Array.Copy(source, index+1, result, index, source.Length - (index+1));

      return result;
   }
} 
示例

int[] N = new int[]{1,0,6,0,3,4};
var result = N.RemoveElement(1);
public static void Main()
{
   int[] N = new int[]{1,0,6,0,3,4};

   Console.WriteLine(string.Join(",", N.RemoveElement(1)));
   Console.WriteLine(string.Join(",", N.RemoveElement(0)));
   Console.WriteLine(string.Join(",", N.RemoveElement(5)));
}
输出

1,6,0,3,4
0,6,0,3,4
1,0,6,0,3


其他资源

从指定位置开始从数组复制元素范围 源索引并将它们粘贴到从 指定的目标索引。指定了长度和索引 作为32位整数


它似乎非常适合一般的扩展方法,并且有一个很好的快速解决方案

注意:这将重新创建一个数组

给定的

public static class Extensions
{
   public static T[] RemoveElement<T>(this T[] source, int index)
      where T : new()
   {

      if(index >= source.Length) throw new ArgumentOutOfRangeException(nameof(index));

      // create new array
      var result = new T[source.Length - 1];
      // Copy the first part
      Array.Copy(source, 0, result, 0, index);
      // Copy the second part
      Array.Copy(source, index+1, result, index, source.Length - (index+1));

      return result;
   }
} 
示例

int[] N = new int[]{1,0,6,0,3,4};
var result = N.RemoveElement(1);
public static void Main()
{
   int[] N = new int[]{1,0,6,0,3,4};

   Console.WriteLine(string.Join(",", N.RemoveElement(1)));
   Console.WriteLine(string.Join(",", N.RemoveElement(0)));
   Console.WriteLine(string.Join(",", N.RemoveElement(5)));
}
输出

1,6,0,3,4
0,6,0,3,4
1,0,6,0,3


其他资源

从指定位置开始从数组复制元素范围 源索引并将它们粘贴到从 指定的目标索引。指定了长度和索引 作为32位整数

您可以使用以下选项:

int[] N = new int[]{1,0,6,0,3,4};

var foos = new List<int>(N);
int indexToRemove = 1;
foos.RemoveAt(indexToRemove);
N = foos.ToArray();

foreach(int elem in N )
    Console.WriteLine(elem);
int[]N=新的int[]{1,0,6,0,3,4};
var foos=新列表(N);
int indexToRemove=1;
foos.RemoveAt(indexToRemove);
N=foos.ToArray();
foreach(N中的int元素)
控制台写入线(elem);
仅供参考:对于高性能/频繁访问,不建议使用linq。

您可以使用以下选项:

int[] N = new int[]{1,0,6,0,3,4};

var foos = new List<int>(N);
int indexToRemove = 1;
foos.RemoveAt(indexToRemove);
N = foos.ToArray();

foreach(int elem in N )
    Console.WriteLine(elem);
int[]N=新的int[]{1,0,6,0,3,4};
var foos=新列表(N);
int indexToRemove=1;
foos.RemoveAt(indexToRemove);
N=foos.ToArray();
foreach(N中的int元素)
控制台写入线(elem);

仅供参考:对于高性能/频繁访问,不建议使用linq。

简短回答:您不能。您可以创建新数组并复制现有元素。但是,按照您的建议执行操作就像从印刷书籍中删除一个单词,然后将下面的所有单词移回原处。如果您需要更改收藏的长度,您应该使用
列表,而不是数组。请向我们展示您的尝试,我们可以帮助您解释为什么不起作用。问“我该怎么做?”而不表现出你的任何企图不是一个好问题。简短回答的可能重复:你不能。您可以创建新数组并复制现有元素。但是,按照您的建议执行操作就像从印刷书籍中删除一个单词,然后将下面的所有单词移回原处。如果您需要更改收藏的长度,您应该使用
列表,而不是数组。请向我们展示您的尝试,我们可以帮助您解释为什么不起作用。问“我该怎么做?”而不表现出你的任何企图不是一个好问题。可能是重复的。因为他坚持数组,所以上面的代码也可以写成
N=Array.FindAll(N,x=>x!=0)这是正确的解决方案,我没有正确阅读问题+1是的。因为他坚持数组,所以上面的代码也可以写成
N=Array.FindAll(N,x=>x!=0)这是正确的解决方案,我没有正确阅读问题+1很好的解决方案,但他要求按值而不是索引删除,因此您的解决方案需要一些调整。很好的解决方案,但他要求按值而不是索引删除,所以您的解决方案需要一些调整。