四联光电智能照明论坛

标题: 怎样删除string[] 定义的数组的元素 [打印本页]

作者: Xiaoxue    时间: 2016-11-1 11:17
标题: 怎样删除string[] 定义的数组的元素
如string[]   arr   =   new   string[]{ "abc1 ", "abc2 ", "abc3 "...};
现在不想要第2(N)个元素了,即arr={ "abc1 ", "abc3 "...};
有什么好的办法实现?

由于:string[] 数组是定长的,一旦初始化了它的长度后就不能更改它的长度了,你只能更改里面的元素,没有删除操作等,如果想做到,只能转换成arraylist!【因此,还不如最开始就采用arraylist了,比较方便,呵呵……】
方法如下:
using   System;
using   System.Collections;

class   Test
{
    static   void   Main()
    {
        string   []   arr   =   {   "abc1 ",   "abc2 ",   "abc3 ",   };
        ArrayList   al     =   new   ArrayList(arr);
        al.RemoveAt(1);
        arr   =   (string   [])al.ToArray(typeof(string));
        foreach   (string   s   in   arr)
        {
            Console.WriteLine(s);
        }
    }
}




欢迎光临 四联光电智能照明论坛 (http://5xhome.com/) Powered by Discuz! X3.2