如何删除阵列选定的元素? [英] How to delete a chosen element in array?

查看:108
本文介绍了如何删除阵列选定的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的任务,我必须从阵列中删除选定的元素,所以我想出了这个code。问题是,假设我有一个数组[1,2,3,4,5,],我想删除1.将输出2,3,4,5,5。这也发生在我选择2
,但是当我选择任何其他数字它不会发生。我在做什么错了?

  strInput =到Console.ReadLine();
的for(int i = 0; I< intAmount;我++)
  {
   如果(strItems [I] == strInput)
    {
      strItems [我] = NULL;
      对于(INT X = 0; X< intAmount-I; X ++)
      {
       I = I + 1;
       strItems [I - 1] = strItems [I];
      }
      intAmount = intAmount - 1;
    }
}


解决方案

我假设您正在使用的字符串数组的基本工作:

  VAR strItems =新的字符串[] {1,2,3,4,5};

在.NET中,该数组总是将是5个元素长。为了消除一个元素,你将不得不在剩余的元素复制到一个新的数组并返回。在一个位置的值设置为不会从数组中删除它。

现在,事情像LINQ这是很容易的(这里没有显示),或者你可以欺骗使用列表与LT;> 收集和做到这一点:

  VAR名单=新名单,LT;串>(strItems);
list.Remove(3);
strItems = list.ToArray();

但我不认为这将教你任何东西。

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

  INT removeIndex = Array.IndexOf(strItems,3);

如果没有被发现的元素,它会返回一个-1,所以做任何事情之前检查这一点。

 如果(removeIndex> = 0)
{
     //继续...
}

最后你要的元素(除了一个我们不希望索引)复制到一个新的数组。所以,干脆,你最终像这样(为注释说明):

 字符串strInput =到Console.ReadLine();
字符串[] = strItems新的字符串[] {1,2,3,4,5};INT removeIndex = Array.IndexOf(strItems,strInput);如果(removeIndex> = 0)
{
    //声明和定义比旧的阵列短的新数组的一个元素
    字符串[] = newStrItems新的字符串[strItems.Length - 1];    //从0循环到新的数组的长度,其中i为位置
    //新阵列中,并且j是旧阵列中的位置
    的for(int i = 0,J = 0; I< newStrItems.Length;我++,J ++)
    {
        //如果该指数等于我们要删除一个,凹凸
        由一个//Ĵ到原始数组中跳过的值
        如果(我== removeIndex)
        {
            J ++;
        }        //从原来的阵列到分配好的元素
        //在适当的位置新数组
        newStrItems [I] = strItems [J]。
    }    //覆盖旧阵列的新
    strItems = newStrItems;
}

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

I have this assignment where I must delete a chosen element from an array, so I came up with this code. The problem is that, suppose I have an array [1,2,3,4,5,], and I want to delete 1. The output would be 2,3,4,5,5. This also happens when I chose 2 , but it does not happen when I chose any other number. What am I doing wrong?

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;
    }
}

解决方案

I'm assuming you are working with a basic array of strings:

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

In .NET, that array is always going to be 5 elements long. In order to remove an element, you are going to have to copy the remaining elements to a new array and return it. Setting the value at a position to null does not remove it from the array.

Now, with things like LINQ this is very easy (not shown here), or you could cheat using the List<> collection and do this:

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

But I don't think that's going to teach you anything.

The first step is to find the index of the element you wish to remove. You can use Array.IndexOf to help you out. Let's find the middle element, "3":

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

If the element was not found, it will return a -1, so check for that before doing anything.

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

Finally you have to copy the elements (except the one at the index we don't want) to a new array. So, altogether, you end up with something like this (commented for explanation):

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;
}

And now strItems will be the new array, minus the value specified for removal.

这篇关于如何删除阵列选定的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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