改变数组大小 [英] change array size

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

问题描述

是否有可能声明之后改变一个数组的大小?
如果没有,是否有任何替代数组?结果
我不希望创建一个大小为1000的数组,但我不知道当我创造它的数组的大小。

Is it possible to change an array size after declaration? If not, is there any alternative to arrays?
I do not want to create an array with a size of 1000, but I do not know the size of the array when I'm creating it.

推荐答案

没有,请尝试使用强类型列表代替。

No, try using a strongly typed List instead.

例如:

而不是使用

int[] myArray = new int[2];
myArray[0] = 1;
myArray[1] = 2;

您可以这样做:

List<int> myList = new List<int>();
myList.Add(1);
myList.Add(2);

列表使用数组来存储数据,因此您能够添加和删除项目,而不必担心改变其大小得到在LinkedList的便利阵列的速度优势。

Lists use array's to store the data so you get the speed benefit of arrays with the convenience of a LinkedList by being able to add and remove items without worrying about changing its size.

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

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