简短的方式来创建数组? [英] Short way to create arrays?

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

问题描述

在VB.NET中,我创建阵列像

 暗淡myArray的=新的ArrayList

不过,是不是有一种方法,而不必让一个变量来创建元素的数组?

在Ruby中,很长的路要走是

  =阵列Array.new

和简单的,非可变的方式只是

  [元素,元素,...]


解决方案

好了,你的东西的可以做原始(和字符串)数组:

 维数组作为新的String()
维数组作为新的String(){一,二,三化}如果(新建字符串(){一,二,三化})。包含(1)然后
    做一些一
万一

如果你移动到2010 VB.NET你会得到一些额外的数组初始化功能,但如果你使用2008或以下,你可以得到你的列表创建可能是这样的最短的:

 昏暗名单新的列表(串)
list.AddRange(新建字符串(){一,二,三化})

和对事物申报的点触控而不为它们分配给一个变​​量:.NET是强类型的,所以当你并不总是要声明一个变量,你的对象将始终需要一个单一类型的(和一个你需要通过指定)。

In VB.NET, I create my arrays like

Dim myArray = New ArrayList

But isn't there a way to create an array with elements without having to make a variable?

In Ruby, the long way was

array = Array.new

And the simple, non-variable way was just

[element,element,...]

解决方案

Well, things you can do with primitive (and String) arrays:

Dim array As New String()
Dim array As New String() { "one", "two", "three" }

If (New String() { "one", "two", "three" }).Contains("one") Then
    ' Do something for "one"
End If

If you move to VB.NET 2010 you will get some extra array initialization features, but if you're using 2008 or below the shortest you can get your lists created might be something like this:

Dim list As New List(Of String)
list.AddRange(New String() { "one", "two", "three" })

And to touch on the point of declaring things without assigning them to a variable: .NET is strongly typed, so while you don't always have to declare a variable, your objects will always need to be of a single type (and one you need to specify through a New).

这篇关于简短的方式来创建数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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