如何在运行时初始化数组大小 [英] How Can I Initialize Array Size During Run Time

查看:104
本文介绍了如何在运行时初始化数组大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道阵列的大小是多少?



如果我正在创建阵列:

I Don't Know What Size Of Array is?

If I Am Creating an Array:

int[] marks = new int[10];





在这种情况下如果用户想要添加超过10,它会丢失错误



而我也不能使用这一个



In This Case If User Want Add More Than 10 , It Will Throw Error

And I Can't Use This One Also

int [] marks = new int[]  { 99,  98, 92, 97, 95};

推荐答案

除了解决方案1:



Volkan Paksoy意味着泛型类 System.Collections.Generic.List<>

https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx [ ^ ]。



根据您真正需要支持的操作,您可能需要其他类型的集合,但不要使用过时的 ArrayList



现在,离你想做的更近,你可以做到这一点, 但是不要't

https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx [ ^ ]。



为什么大胆不要?阅读备注部分。它会在每次重新分配时创建一个全新的数组对象,并复制所有数据,但效果会很糟糕,以此成本,甚至在功能上都不可接受。你必须决定要添加多少元素;面对任意决定的需要是不好的;然后你添加10并且用户只使用1,此外,你必须保持实际使用元素的数量,而不是分配。收藏品,收藏品,只收藏品。



由于类似的原因,从不重复连接相同的字符串,它不一样。字符串是不可变的 System.Text.StringBuilder ,可以视为字符串的可变模拟。



- SA
In addition to Solution 1:

Volkan Paksoy meant the generic class System.Collections.Generic.List<>:
https://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

Depending on what operations you really need to support, you may need other types of collections, but don't use obsolete ArrayList.

Now, closer to what you wanted to do you can do this, but don't:
https://msdn.microsoft.com/en-us/library/bb348051%28v=vs.110%29.aspx[^].

Why "big bold don't"? Read "Remarks" section. It would create a brand new array object each time you reallocate it, amd copy all data, but the effect would be miserable, at this cost, it is not even functionally acceptable. You have to decide how many elements to add; and it's bad to face a need for an arbitrary decision; then you add 10 and the user only uses 1, besides, you will have to maintain the number of actually uses elements, as opposed to allocated. Collections, collections, only collections.

By a similar reasons, never repeatedly concatenate "the same string", it's not the same. String is immutable. System.Text.StringBuilder, which can be considered as a mutable analog of string.

—SA


您可以使用List< int>而不是像这样的数组:



You can use a List<int> instead of an array like this:

var myList = new List<int>();





您可以添加或删除尽可能多的您在运行时想要的项目。如果你肯定需要从中获取数组,你可以用ToArray方法转换它:





You can add or remove as many items you want at runtime. If you definitely need to get an array out of it you can convert it with ToArray method:

var myArray = myList.ToArray();


嗯,你不能。

但你可以将你的数组转换为列表,然后将项目附加到它。



类似的东西,

Well, you cant.
But you can convert your array to list and then append item to it.

Something like,
int[] marks = new int[10];
// fill your array with the values
List<int> MarksList = new List<int>(marks);
marksList.Add(75);



使用此List<t>构造函数 [ ^ ]





< int> 标签已删除



-KR


Use this List<t> Constructor[^]


<int> tags removed

-KR


这篇关于如何在运行时初始化数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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