删除数组中的项目 [英] Remove item in array

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

问题描述

我有一个列表框,我已经制作了一个数组...



I have a list box which I have made an array from ...

public string[] getlistboxarray()
{
    string[] arr = new string[listBox2.Items.Count];
    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        arr[i] = listBox2.Items[i].ToString();
    }
    return arr;
}





我有一个开始按钮,当我点击它时会删除数组中的第一项。我该怎么做?



我尝试过:



arr = arr.ToList()。RemoveAt(0)。ToArray();



I have a start button, which when I click it removes the first item in the array. How would I do this?

What I have tried:

arr = arr.ToList().RemoveAt(0).ToArray();

推荐答案

请先阅读我对该问题的评论。



如果要从列表框中删除项目,请使用: ListBox.ObjectCollection.RemoveAt方法(Int32)(System.Windows.Forms) [ ^ ]

如果要从数组中删除项目,请使用: Array.removeAt功能 [ ^ ]
Please, read my comment to the question first.

If you want to delete item from listbox, use: ListBox.ObjectCollection.RemoveAt Method (Int32) (System.Windows.Forms)[^]
If you want to remove item from array, use: Array.removeAt Function[^]


如果你想操纵它e数组,一种简单的方法是将数据复制到数组变量中,然后调整它的大小。



例如

If you want to manipulate just the array, one simple way is to copy the data in the into the array variable and then resize it.

For example
Array.Copy(arr, 1, arr, 0, arr.Length - 1);
Array.Resize(ref arr, arr.Length - 1);



ADDED

-----

或者Paolo Zemek建议


ADDED
-----
Or as Paolo Zemek suggested

string[] arr2 = new string[listBox2.Items.Count - 1];
Array.Copy(arr, 1, arr2, 0, arr.Length - 1);


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

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