在 VB.NET 中在运行时调整数组大小 [英] Resizing an array at runtime in VB.NET

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

问题描述

在运行时的 Windows 窗体应用程序中,每次添加元素时,我都会调整数组的大小.所以首先我必须将大小调整为 size + 1,然后向该索引添加一个成员.我该怎么做?

In my Windows Forms application at runtime I will be resizing an array each time I add an element. So first I must resize to the size + 1, and then add a member to this index. How do I do this?

推荐答案

可以使用 ReDim 语句,但这确实不是您的最佳选择.如果您的数组经常改变大小,尤其,因为听起来您只是在追加,您可能应该使用通用的 List(Of T) 或类似的集合类型.

You could use the ReDim statement, but this really isn't your best option. If your array will be changing sizes often, especially as it sounds like you're just appending, you should probably use a generic List(Of T) or similar collection type.

你可以像使用数组一样使用它,另外在末尾添加一个项目就像MyList.Add(item)

You can use it just like you use an array, with the addition that adding an item to the end is as easy as MyList.Add(item)

要使用通用列表,请将 Imports System.Collections.Generics 添加到文件顶部.然后,您可以像这样声明一个新的整数列表:

To use a generic list, add Imports System.Collections.Generics to the top of the file. Then, you would declare a new integer list like this:

Dim MyList As New List(Of Integer)()

或者像这样的字符串列表:

or a string list like this:

Dim MyList As New List(Of String)()

你应该明白了.

这篇关于在 VB.NET 中在运行时调整数组大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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