怎样增加sting数组的长度?谁能解答给谁加分!

cooldht 2002-11-01 03:04:22
string[] childID=new string[5];
…………
给数组赋值
…………
int length=childID.Length;

现在我要增加sting数组的长度,将childID[length]赋值,
而原来数组的内容不能丢失!
怎么办?
...全文
117 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dy_2000_abc 2002-11-01
  • 打赏
  • 举报
回复
使用StringCollection类
snewxf 2002-11-01
  • 打赏
  • 举报
回复
同意楼上!
project 2002-11-01
  • 打赏
  • 举报
回复
如果是用string[]恐怕只能再声明一个更长的string[],然后将数据一一复制到新的数组中,让后释放旧数组。
如果是用ArrayList等Collection类,就简单了。不用管length,直接add就可以了!
yarshray 2002-11-01
  • 打赏
  • 举报
回复
arraylist
using System;
using System.Collections;
public class SamplesArrayList {

public static void Main() {

// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add("Hello");
myAL.Add("World");
myAL.Add("!");

// Displays the properties and values of the ArrayList.
Console.WriteLine( "myAL" );
Console.WriteLine( "\tCount: {0}", myAL.Count );
Console.WriteLine( "\tCapacity: {0}", myAL.Capacity );
Console.Write( "\tValues:" );
PrintValues( myAL );
}

public static void PrintValues( IEnumerable myList ) {
System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
while ( myEnumerator.MoveNext() )
Console.Write( "\t{0}", myEnumerator.Current );
Console.WriteLine();
}
}
/*
This code produces the following output.

myAL
Count: 3
Capacity: 16
Values: Hello World !
*/

111,116

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧