-1

我有这个 int 数组:

0,1
0,2
0,3
0,4
1,5
1,6
1,7
1,8
etc

我需要做的是创建一个函数,给我一个类似这样的数组

0,1
0,2
0,3
0,4

它返回索引 0 中具有相同数字的所有结果。下次我调用它时,它应该返回:

1,5
1,6
1,7
1,8

等等。或者它也可以返回这个

0,1,2,3,4
1,5,6,7,8

等等

请帮忙 :(

4

2 回答 2

1

假设你有类似的东西

Dim variable(,) as Integer '(2-dimensional array)

那么你可以使用一个简单的循环:

Dim _result as New List(Of String)
For i as Integer = 0 to 1
  For j as Integer = 1 to 4
    _result.Add(variable(i, j+i*4).ToString)
  Next j
  _result.Add(vbCrLf)
Next i
MsgBox(Join(_result.ToArray,","))

这有点脏,最好将每一行合并起来并与 vbCrLf 进行另一次连接,因为这样你在每一行前面都有另一个逗号,但它应该足以理解它是如何工作的。

更新 了好吧,我怀疑您是否需要阵列中的那些地方,但答案不适合它。4 个元素,所以第二个数组只能用 1 到 4 加上 4 * 第一个数组索引来访问

于 2013-04-09T15:13:51.940 回答
0

数组很棒,但您可能还想尝试使用更“现代”的对象,如字典和列表

尝试这个:

' Load the data
Dim x As New Dictionary(Of Integer, List(Of Integer))
x.Add(1, New List(Of Integer)({1, 2, 3, 4}))
x.Add(2, New List(Of Integer)({5, 6, 7, 8}))


' Access the data
Debug.Print("items under 1")
For Each i As Integer In x.Item(1)
    Debug.Print(i)
Next

Debug.Print("items under 2")
For Each i As Integer In x.Item(2)
    Debug.Print(i)
Next

这给出了如下输出:

items under 1
1
2
3
4
items under 2
5
6
7
8
于 2013-04-09T20:29:46.970 回答