2010年9月28日 星期二

VB.net 不規則陣列

'宣告不規則陣列的格式
Dim a()() As String = New String(4)() {}

a(0) = New String() {"A", "B", "C", "D", "E"}
a(1) = New String() {"A", "C", "D"}
a(2) = New String() {"D", "E"}
a(3) = New String() {"F"}

'若要新增一個陣列的[內容]時,可使用以下方式
Dim mylist As New List(Of String)
mylist.Add("ss1")
mylist.Add("ss2")
a(4) = mylist.ToArray



'讀取不規則陣列
'以For Next
For i = 0 To a.Length - 1
For j = 0 To a(i).Length - 1
Debug.Print(a(i)(j))
Next
Next


'以For Each
Dim achild ()
Dim ChildStr As String
For Each achild In a
For Each ChildStr In achild
Debug.Print( ChildStr )
Next

Next


'若一開始 不指定 陣列大小,動態處理則....
Dim a()() As String = New String()() {}
ReDim a(3)

'若一開始沒有ReDim 陣列大小,會發生問題,若資料增加後才發現陣列不夠,請加上Preserve辜關鍵字,如下
ReDim Preserve a(3)

沒有留言:

張貼留言

追蹤者