c# 上的菜鸟问题:如何创建一维动态数组?以及以后怎么改?
谢谢。
最佳答案
您可以在 C# 中使用 List<>
对象,而不是使用数组。
List<int> integerList = new List<int>();
要迭代列表中包含的项目,请使用
foreach
运算符:foreach(int i in integerList)
{
// do stuff with i
}
您可以使用
Add()
和 Remove()
函数在列表对象中添加项目。for(int i = 0; i < 10; i++)
{
integerList.Add(i);
}
integerList.Remove(6);
integerList.Remove(7);
您可以使用
List<T>
函数将 ToArray()
转换为数组:int[] integerArray = integerList.ToArray();
这是
List<>
对象上的 documentation。关于c# - 如何在c#中创建一维动态数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2676291/