vb.net 在 Visual Basic 中列出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46322409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 20:20:05  来源:igfitidea点击:

List in Visual Basic?

vb.netconsole-application

提问by Fridge Thrower

I am new to Visual Basic and i need help.

我是 Visual Basic 的新手,需要帮助。

I have previously used python where you simply create a list of items by simply doing:

我以前使用过 python,您只需执行以下操作即可创建项目列表:

list = [item1, item2]

列表 = [项目 1,项目 2]

But i have no idea how I can do this in Visual Basic.

但我不知道如何在 Visual Basic 中做到这一点。

Please can someone help me to simply create a list, like you can in python, but in Visual Basic?

请有人帮我简单地创建一个列表,就像在 python 中一样,但在 Visual Basic 中?

回答by Sasha

dim list as item() = {item1, item2}

The () next to item signify that it is an array.

item 旁边的 () 表示它是一个数组。

A working example of an integer list:

整数列表的工作示例:

Dim list As Integer() = {1, 2, 3}

These lists are refered to as "arrays" though.

不过,这些列表被称为“数组”。

If you want an actual list, you can do:

如果你想要一个实际的列表,你可以这样做:

Dim list As New List(Of Integer)({1,2,3})

This one allows you to access .Add and .AddRange, and does not hold a static capacity.

这允许您访问 .Add 和 .AddRange,并且不具有静态容量。

回答by Hahayahahay

  Dim arraylist As New ArrayList
  arraylist.Add("item")

You can use this. It's a dynamic list

你可以用这个。这是一个动态列表

回答by Plaguedriver

A list in Python would essentially be an Array in Visual Basic.

Python 中的列表本质上是 Visual Basic 中的数组。

You would do this by saying Dim list(x) As Integerand then adding either the size of the list (in the parenthesis) or using a ReDimstatement to create a dynamic array if you do not know what the initial size of the array is going to be.

您可以通过说Dim list(x) As Integer然后添加列表的大小(在括号中)或使用ReDim语句来创建动态数组(如果您不知道数组的初始大小将是多少)来做到这一点。