在数组中添加值 [英] adding values in the array

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

问题描述



请提出建议,如何为我的3D阵列增加价值.

我用来显示错误未处理索引超出范围异常"的代码

Hi,

Please advice how can i add value to my 3d array.

the code which i used showing error "index outof range exception was unhandled"

arrdays(y, m, d) = (d)& "/" & (m) & "/" & (y))

推荐答案

您需要查看声明数组的位置以及为其分配内存空间的位置.
然后,您需要查看y,m,d变量的值,并检查是否已留有足够的空间,否则它们将超出分配的内存之外,您将收到此错误消息.

注意:3D阵列变得非常大,非常快-它们以立方级数扩展.因此,如果您的"y"代表"Year",那么您可能要考虑使用Dictionary或List,就好像您分配了一个数组以保存到2050年一样,您可能不会使用前2000个条目-但它们每个都分配了12 * 31个元素...
You need to look at where you declare the array, and where you assign memory space to it.
Then you need to look at the values of your y, m, d variables and check that you have allowed enough space, or they will go outside the memory you have allocated, and you will get this error message.

NOTE: 3D arrays get very big, very quickly - they expand in a cubic progression. So if your "y" is for "Year" then you may want to consider using a Dictionary, or a List instead, as if you allocate an array to hold up to 2050, you probably won''t use the first 2000 entries - but they will each have 12 * 31 elements allocated...


您的代码中还有一个问题:重复使用字符串串联(&"字符串运算符).字符串是不可变的,因此每个串联都涉及创建一个全新的字符串实例以及来回复制数据.您需要对固定数量的字符串操作数使用string.Format,对于可变的System.Text.StringBuilder则需要使用可变的System.Text.StringBuilder,例如在循环等情况下.

—SA
One more problem in your code: using string concatenation (''&'' string operator) repeatedly. Strings are immutable, so every single concatenation involves creation of a brand new string instance with copying data back and forth. You need to use string.Format for a fixed number of string operand, and mutable System.Text.StringBuilder otherwise, in cases like cycles, etc.

—SA


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

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