C#中如何从结构体数组中删除一个项目

发布于 2024-11-09 07:23:29 字数 725 浏览 0 评论 0原文

我有一个 C# 结构体,我根据我在此处表达的代码定义了结构体的数组列表。我在数组列表中添加了项目,但我也需要从列表中删除几行。你能帮我如何从我的结构数组列表中删除一个或多个项目吗:

public struct SwitchList
    {
        public int m_Value1, m_Value2;
        public int mValue1
        {
            get  { return m_Value1;  }
            set  {m_Value1 = value; }
        }

        public int mValue2
        {
            get  { return m_Value2;  }
            set  {m_Value2 = value; }
        }       
   }

   //Define an array list of struct
   SwitchList[] mSwitch = new SwitchList[10]; 

   mSwitch[0].mValue1=1;
   mSwitch[0].mValue2=2;

   mSwitch[1].mValue1=3;
   mSwitch[1].mValue2=4;

   mSwitch[2].mValue1=5;
   mSwitch[2].mValue2=6;

现在我如何删除我的一个项目,例如项目 1。 谢谢。

I have a struct in C# and I define and array list of my struct based on my code that I express here. I add items in my array list, but I need to delete a few rows from my list too. Could you help me how can I delete item or items from my struct array list:

public struct SwitchList
    {
        public int m_Value1, m_Value2;
        public int mValue1
        {
            get  { return m_Value1;  }
            set  {m_Value1 = value; }
        }

        public int mValue2
        {
            get  { return m_Value2;  }
            set  {m_Value2 = value; }
        }       
   }

   //Define an array list of struct
   SwitchList[] mSwitch = new SwitchList[10]; 

   mSwitch[0].mValue1=1;
   mSwitch[0].mValue2=2;

   mSwitch[1].mValue1=3;
   mSwitch[1].mValue2=4;

   mSwitch[2].mValue1=5;
   mSwitch[2].mValue2=6;

Now how can I delete one of my items, for example item 1.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尤怨 2024-11-16 07:23:29

数组是固定长度的数据结构。

您将需要创建一个新数组,其大小比原始数组小一,并将除要删除的数组之外的所有项目复制到其中,并开始使用新数组而不是原始数组。

为什么不使用 List反而?它是一个动态结构,允许您添加和删除项目。

Arrays are fixed length data structures.

You will need to create a new array, sized one less than the original and copy all items to it except the one you want to delete and start using the new array instead of the original.

Why not use a List<T> instead? It is a dynamic structure that lets you add and remove items.

岛歌少女 2024-11-16 07:23:29

您将需要移动元素并调整数组的大小(这很昂贵),因为存在一些复杂性,您需要将其隐藏在仅显示集合的类中,而不暴露其存储方式的实现细节。幸运的是,Microsoft 已经提供了一个名为 List 的类,该类与 System.Collections.Generic 命名空间中的其他一些集合类型一起满足了最常见的集合需求。

作为旁注,您应该使用自动属性而不是您拥有的简单属性样式

You will need to move elements around and resize the array (which is expensive), since there is some complexity there you going to want to hide it in class that just presents the collection without exposing the implementation details of how its stored. Fortunately Microsoft has already provided a class that does just this called List<T> which along with a few other collection types in System.Collections.Generic namespace meet most common collection needs.

as a side note, you should use auto-properties instead of the trivial property style that you ha

妄想挽回 2024-11-16 07:23:29

这是不可能的,因为数组是固定大小的元素块。因为结构体是值类型而不是引用类型,所以您也不能只将元素 zo 设置为 null。一种选择是创建一个新的较小数组并将剩余值复制到新数组。但在我看来,更好的方法是使用列表。

That's not possible, because an array is a fixed size block of elements. Because structs are values types and not reference types, you also can't just set the element zo null. One option would be to create a new smaller array and to copy your remaining values to the new array. But the better approach would be to use a List in my opinion.

梦纸 2024-11-16 07:23:29

如果您真的、真的想要使用数组并移动事物,这里有一些如何做到这一点的示例:

{
    // Remove first element from mSwitch using a for loop.
    var newSwitch = new SwitchList[mSwitch.Length - 1];
    for (int i = 1; i < mSwitch.Length; i++)
        newSwitch[i - 1] = mSwitch[i];
    mSwitch = newSwitch;
}
{
    // Remove first element from mSwitch using Array.Copy.
    var newSwitch = new SwitchList[mSwitch.Length - 1];
    Array.Copy(mSwitch, 1, newSwitch, 0, mSwitch.Length - 1);
    mSwitch = newSwitch;
}

If you really, really want to use arrays and move things around, here are some examples of how to do it:

{
    // Remove first element from mSwitch using a for loop.
    var newSwitch = new SwitchList[mSwitch.Length - 1];
    for (int i = 1; i < mSwitch.Length; i++)
        newSwitch[i - 1] = mSwitch[i];
    mSwitch = newSwitch;
}
{
    // Remove first element from mSwitch using Array.Copy.
    var newSwitch = new SwitchList[mSwitch.Length - 1];
    Array.Copy(mSwitch, 1, newSwitch, 0, mSwitch.Length - 1);
    mSwitch = newSwitch;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文