2011-11-07 46 views
11

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

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时,也会发生这种情况,但是当我选择任何其他号码时不会发生这种情况。

我在做什么错?

+2

什么是'intAmount'和'strItems'?你正在做的for循环,这通常是一个坏主意内大量的for循环指数操纵的。 –

+0

你能否澄清你的问题:你说[1,2,3,4,5],删除1的操作变成了[2,3,4,5,5]。你如何选择5作为要复制的元素? – ssamuel

+0

strItems是字符串数组,intAmount是元件的数量和strInput是用户的动产删除的元素。 – user1033065

回答

33

我假设你用字符串的基本阵列工作:

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

在.NET中,该阵列是始终将是5个元素长。为了移除一个元素,你将不得不将其余的元素复制到一个新的数组中并返回它。将位置上的值设置为null不会将其从阵列中删除。

现在,随着东西像LINQ,这很简单(这里没有显示),或者你可以欺骗使用List<>收集和做到这一点:

var list = new List<string>(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将成为新的阵列,减去去除指定的值。

+0

哇!什么是完整的和解释性的答案! – zazkapulsk

1
  • 数组是一个固定大小的,你不能缩短其长度,而无需创建一个新的数组。你所能做的只是将有效的元素的长度存储在数组中(即在删除1后长度为4)。

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

  • 于使用阵列的方法是使用一个收集诸如ArrayList将照顾调整大小,删除和保持物品的量的计数在它的,再加上多很多。

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

3

数组的大小是固定的 - 初始化一次只能修改的项目,但您不能添加或删除项目。如果你想删除一个集合中的项目,你有两个选择:

1)创建一个具有原始阵列减去你要删除一个所有成员的新数组。

2.)使用可调整大小的收集类型,并允许添加或删除项目,如List<T>(在您的案例中为List<int>)。如果你的收藏不是静态的,这就是你在“现实世界”中所做的。

3

在您的具体实现我觉得你错过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; 
} 

如果找到指定的falue,该函数将返回true,否则返回false。 ,你在你的例子说明它会将所有的物品,当然,也不会改变数组的大小。

阵列是固定的大小。 你不能改变数组的大小,只是语言不允许这样做。 数组是,而且将永远是固定的大小!

从阵列,你应该做的事情这个删除项目:

public static T[] RemoveAt<T>(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); 

此功能将创建一个包含除一个指定索引的所有元素的数组。

现在,为什么有人会做这样的事情,而不是使用列表或字典或观察者? 直接使用列表而不使用数组。

2

可以使用除方法来过滤数据

AllData = {10, 30, 20, 50} 

FilterData = {30, 20} 

Result = AllData.Except(​FilterData) 

结果将{10, 50}