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