2011年8月23日

VB.NET如何轉一維陣列成二維

如果你想要把一維陣列成二維,可參考以下:

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim ar1() As String = {"Tom", "John", "Marry", "May"} ' 填入一維陣列資料
Const ar2ColLength As Integer = 2 ' 二維陣列最大欄數

Dim ar2RowLength As Integer = 0
ar2RowLength = Math.Ceiling(ar1.Length / ar2ColLength) ' 二維陣列最大列數

Dim ar2(,) As String
ReDim Preserve ar2(ar2RowLength, ar2ColLength) '建立二維陣列

For i As Integer = 0 To ar2RowLength - 1 '資料從一維搬到建立二維陣列

For j = 0 To ar2ColLength - 1
ar2(i, j) = ar1(i)
Next

Next

Me.Label1.Text = ar2(1, 1) ' 驗證二維陣列(1,1) = 'John'
End Sub
End Class

沒有留言:

張貼留言