VB.Net中的队列数组

时间:2021-06-15 21:20:10

Probably a very dumb question, but I want to create an array of queues in vb.net - so I can reference each queue with an index:

可能是一个非常愚蠢的问题,但我想在vb.net中创建一个队列数组 - 所以我可以用索引引用每个队列:

eg

commandQueue(1).enqueue("itemtext")

commandQueue(2).enqueue("othertext")

where commandQueue(1) refers to a separate queue than commandQueue(2)

其中commandQueue(1)引用的单独队列不是commandQueue(2)

I've got all tangled up trying to define an object array and put queues in.

我已经纠缠不清试图定义一个对象数组并放入队列。

Yes of course I can do it with old fashioned arrays, pointers etc, doing the management by hand, but this seemed so much more elegant...

是的,我当然可以用老式的数组,指针等做,手工管理,但这看起来更优雅......

1 个解决方案

#1


3  

What's wrong with this solution?

这个解决方案有什么问题?

Dim commandQueue As Queue(Of T)()

There's nothing “old fashioned” about this solution. However, dynamic memories might sometimes be better suited:

这个解决方案没什么“老式”的。但是,动态记忆有时可能更适合:

Dim commandQueue As New List(Of Queue(Of T))()

In both cases, you need to initialize each queue before using it! In the case of an array, the array has to be initialized as well:

在这两种情况下,您需要在使用之前初始化每个队列!在数组的情况下,数组也必须初始化:

' Either directly: '
Dim commandQueue(9) As Queue(Of T)
' or, arguably clearer because the array length is mentioned explicitly: '
Dim commandQueue As Queue(Of T)() = Nothing ' `= Nothing` prevents compiler warning '
Array.Resize(commandQueue, 10)

#1


3  

What's wrong with this solution?

这个解决方案有什么问题?

Dim commandQueue As Queue(Of T)()

There's nothing “old fashioned” about this solution. However, dynamic memories might sometimes be better suited:

这个解决方案没什么“老式”的。但是,动态记忆有时可能更适合:

Dim commandQueue As New List(Of Queue(Of T))()

In both cases, you need to initialize each queue before using it! In the case of an array, the array has to be initialized as well:

在这两种情况下,您需要在使用之前初始化每个队列!在数组的情况下,数组也必须初始化:

' Either directly: '
Dim commandQueue(9) As Queue(Of T)
' or, arguably clearer because the array length is mentioned explicitly: '
Dim commandQueue As Queue(Of T)() = Nothing ' `= Nothing` prevents compiler warning '
Array.Resize(commandQueue, 10)