2013-04-09 57 views
-1

我有這樣的int數組:int數組vb.net

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 

幫助請:(

+2

你有什麼迄今所做? – 2013-04-09 15:05:45

+0

你也可以提供int數組的聲明嗎?是否像Dim變量(,)爲整數? – Amegon 2013-04-09 15:06:04

+0

是的它是昏暗的變量(,)作爲整數 – 2013-04-09 15:07:10

回答

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 *第一數組索引

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