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

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

问题描述

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

I have this assignment where I must delete a chosen element from an array, so I came up with this code:

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

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 choose 2, but it does not happen when I choose any other number.

我做错了什么?

推荐答案

我假设您正在处理一个基本的字符串数组:

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

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

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

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.

现在,使用 LINQ 之类的东西非常容易(此处未显示),或者您可以使用 List<> 集合作弊并执行以下操作:

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.

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

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");

如果没有找到该元素,它会返回一个 -1,所以在做任何事情之前都要检查一下.

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

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

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

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

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