如何删除数组中的选定元素?

我有这个任务,我必须从数组中删除一个选定的元素,所以我想出了这个代码:

strInput = Console.ReadLine(); for (int i = 0; i < intAmount; i++) { if (strItems[i] == strInput) { strItems[i] = null; for (int x = 0; x < intAmount-i; x++) { i = i + 1; strItems[i - 1] = strItems[i]; } intAmount = intAmount - 1; } } 

问题是,假设我有一个数组[1,2,3,4,5,] ,我想删除1 。 输出将是[2,3,4,5,5] 。 当我选择2时也会发生这种情况,但是当我选择任何其他数字时它不会发生。

我究竟做错了什么?

我假设你正在使用一个基本的字符串数组:

 var strItems = new string[] { "1", "2", "3", "4", "5" }; 

在.NET中,该数组总是长5个元素。 要删除元素,您必须将其余元素复制到新数组并返回它。 将某个位置的值设置为null不会将其从数组中删除。

现在,像LINQ这样的东西很容易(这里没有显示),或者你可以使用List<>集合作弊并执行此操作:

 var list = new List(strItems); list.Remove("3"); strItems = list.ToArray(); 

但我认为这不会教会你什么。

第一步是找到要删除的元素的索引。 您可以使用Array.IndexOf来帮助您。 让我们找到中间元素“3”:

 int removeIndex = Array.IndexOf(strItems, "3"); 

如果找不到该元素,它将返回-1,因此在执行任何操作之前检查该元素。

 if (removeIndex >= 0) { // continue... } 

最后,您必须将元素(除了我们不想要的索引之外的元素)复制到新数组。 所以,总而言之,你最终得到这样的东西(评论说明):

 string strInput = Console.ReadLine(); string[] strItems = new string[] { "1", "2", "3", "4", "5" }; int removeIndex = Array.IndexOf(strItems, strInput); if (removeIndex >= 0) { // declare and define a new array one element shorter than the old array string[] newStrItems = new string[strItems.Length - 1]; // loop from 0 to the length of the new array, with i being the position // in the new array, and j being the position in the old array for (int i = 0, j = 0; i < newStrItems.Length; i++, j++) { // if the index equals the one we want to remove, bump // j up by one to "skip" the value in the original array if (i == removeIndex) { j++; } // assign the good element from the original array to the // new array at the appropriate position newStrItems[i] = strItems[j]; } // overwrite the old array with the new one strItems = newStrItems; } 

现在strItems将是新数组,减去指定的删除值。

C#中的数组具有固定大小 – 一旦初始化,您只能修改项目,但不能添加或删除项目 。 如果要从集合中删除项目,您有两个选择:

1.)创建一个新数组,其中包含原始数组的所有成员减去要删除的数组。

2.)使用可resize的集合类型,并允许添加或删除List (在您的情况下为List )之类的项目。 如果您的集合不是静态的,那么您将在“真实世界”中执行此操作。

在你的具体实施中,我想你会错过一个break; 语句,当你完成内循环时,你应该从外循环出去。 赋值为null根本没用。

如果列表只是一个数字列表,为什么你使用字符串? 如果是这种情况,请直接使用整数。

如果你只需要删除一个元素,你的练习似乎会问这样的事情。

 public bool MyDelete(int[] array, int value) // Easy to do for strings too. { bool found = false; for (int i = 0; i < array.Length; ++i) { if (found) { array[i - 1] = array[i]; } else if (array[i] == value) { found = true; } } return found; } 

如果找到指定的值,则此函数将返回true,否则返回false。 它将移动您在示例中描述的所有项目,但当然,它不会更改数组的大小。

数组是固定大小的。 你不能改变数组的大小,简单地说,语言不允许这样做。 arrays是,过去和将来都是固定的大小!

要从数组中删除项目,您应该执行以下操作:

 public static T[] RemoveAt(T[] array, int index) // hope there are not bugs, wrote by scratch. { int count = array.Length - 1; T[] result = new T[count]; if (index > 0) Array.Copy(array, 0, result, 0, index - 1); if (index < size) Array.Copy(array, index + 1, result, index, size - index); return result; } ... strItems = RemoveAt(strItems, index); 

此函数将创建一个新数组,其中包含除指定索引处的元素之外的所有元素。

现在,为什么有人会做这样的事情,而不是使用List或Dictionary或wathever? 直接使用List而不使用数组。

可以使用Except方法过滤数据

 AllData = {10, 30, 20, 50} FilterData = {30, 20} Result = AllData.Except(​FilterData) 

结果将是{10, 50}

  • 数组是固定大小的,你不能在不创建新数组的情况下缩短它们的长度。 您所能做的就是将有效元素的长度存储在数组中(即,删除1 ,长度为4)。

    另外,我不确定数组中元素的顺序是否重要,但如果不是,则可以交换第一个和最后一个元素,而不是移动每个元素后移除前向1个位置。

  • 使用数组的另一种方法是使用一个集合,例如ArrayList ,它将负责resize,删除并保持其中项目数量的计数,以及更多。

  • 但是,由于这是作业,您可能必须使用数组。 使用变量跟踪长度,而不是使用array.length ,或者每次要更改大小时创建新数组。 如果您不必使用数组,那么请查看可以在C#中使用的集合。