创建数组时何时需要使用“New"关键字? [英] When do I need to use a `New` keyword when creating an array?

查看:37
本文介绍了创建数组时何时需要使用“New"关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在查看 VB 中的数组的

声明 sales 的行实际上做了两件容易混淆的事情.可以这样声明:

' 有时我会想要一个名为 sales 的双打数组:Dim sales()() As Double' 好的,我需要它来保存 12 个双数组:销售 = 新双 (11)() {}

第一步声明变量和类型.第 2 步通过创建临时数组并分配它来定义容器数组".(该链接试图用 inquiriesByYearMonthDay 来说明这一点 - 它在前面的块中声明.)

销售还没有准备好使用,它只是一堆Nothing.

' 好的,我需要第一个数组来容纳 5 个元素sales(0) = New Double(4) {} ' 基于 0,所以 4 == 5 项sales(0)(0) = 12.67 ' 最后我们可以使用它

现在,sales(0)(0 到 4) 可以存储数据.sales(1)() 和其余的尚未初始化.

New 并没有真正直接作用于 sales.它正在创建一个新的临时数组(在右侧),该数组立即分配给左侧 sales() 中的一个插槽.

关键是:数组在给定大小后就可以使用了.因此,它们在功能上是相同的:

' 创建具有 5 个插槽的 int 数组:Dim numbers(4) 作为整数' 声明数组;为其分配空的 4 槽 int 数组:Dim numbers() As Int32 = New Int32(4) {}

第一个声明包含大小;第二个为其分配一个指定大小的(新)临时数组.也就是说,VB 可以推断出大小:

Dim fishes As String() = {"cod", "salmon", "tuna", "perch", "barracuda"}

{...} 是另一个临时数组,VB 将其分配给fishes".

<小时>

但是数组很笨重.sales(6,3) 代表什么?为了使它们可读,你需要在任何地方使用常量:foo = sales(SALES_MARCH, SALES_ZIGGY).就个人而言,我认为唯一好的数组是具有已知固定值的静态数组:

Dim Days As String() = {"sun", "mon", "tues"....}

否则,List(Of T)Dictionary(TK, TV)Collection(Of T) 和许多其他更容易使用、创建、管理和使用(他们自己调整大小!)

I was just looking at the help page for arrays in VB and apparently all of these are acceptable syntax for creating arrays:

Dim numbers(4) As Integer 

inquiriesByYearMonthDay = New Byte(20)()() {}

Dim sales()() As Double = New Double(11)() {}

Note that the first one doesn't even have a New keyword, the second one only has a New keyword, and the third one uses both the Dim and New keywords. Why is there so much inconsistency and which one are you supposed to use? I'm most curious about the first one since the array object is never even being created

解决方案

Actually, only the first array is created and ready for use. The others are merely declared as arrays (or partially created).

Dim numbers(4) As Integer
Dim sales()() As Double = New Double(11)() {}

As Intellisense shows, numbers is ready to use. For the jagged array sales - an array of arrays- the code has so far just told it how many arrays it will hold. Each of those is Nothing because the code has not created or assigned one to any of the slots:

The line declaring sales is actually doing two things which can be confusing. It could have been declared like this:

' at some point I will want an array of array of doubles named sales:
Dim sales()() As Double

' Ok, I need it to hold 12 double arrays:
sales =  New Double(11)() {}

Step one declares the variable and Type. Step 2 defines the "container array" by creating a temp array and assigning it. (The link tries to illustrate this with inquiriesByYearMonthDay - it is declared in the preceding block.)

Sales still isnt ready to use, it just a bunch of Nothing.

' Ok, I need the first array to hold 5 elements
sales(0) = New Double(4) {}       ' 0 based, so 4 == 5 items
sales(0)(0) = 12.67               ' FINALLY we can use it

Now, sales(0)(0 thru 4) can store data. sales(1)() and the rest have not been initialized.

The New isnt really acting on sales directly. It is creating a new, temp array (on the right) which is immediately assigned to a slot in sales() on the left.

The key is: arrays are ready to use when given a size. So, these are functionally identical:

' create int array with 5 slots:
Dim numbers(4) As Integer
' declare array; assign empty 4 slot int array to it:
Dim numbers() As Int32 = New Int32(4) {}

The first declaration includes the size; the second assigns a (new) temp array of a specified size to it. That said, VB can infer the size:

Dim fishes As String() = {"cod", "salmon", "tuna", "perch", "barracuda"}

The {...} is another temp array, which VB assigns to 'fishes'.


But arrays are just clunky. What will sales(6,3) represent? To make them readable you need constants everywhere: foo = sales(SALES_MARCH, SALES_ZIGGY). Personally, I think the only good array is a static array with known, fixed values:

Dim Days As String() = {"sun", "mon", "tues"....}

Otherwise, List(Of T), Dictionary(TK, TV), Collection(Of T) and many many others are easier to use, create, manage and work with (they size themselves!)

这篇关于创建数组时何时需要使用“New"关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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