Function OneD2TwoD(ByVal xLen As Integer, ByVal yLen As Integer)
' (1) populate 1d array
Dim TwoD(xLen, yLen) As Integer
Dim OneD((xLen + 1) * (yLen + 1) - 1) As Integer
For i As Integer = 0 To OneD.GetUpperBound(0)
OneD(i) = i
Next
PrintValues(OneD, "|")
Console.WriteLine()
' (2) Convert 1d array to 2d array/
Dim z, Row(yLen) As Integer
For x As Integer = 0 To xLen
For y As Integer = 0 To yLen
z = x * (yLen + 1) + y
TwoD(x, y) = OneD(z)
Row(y) = TwoD(x, y)
Next
PrintValues(Row, "|")
Next
Console.WriteLine()
' (3) Convert 2d array to 1d array/
Erase OneD
ReDim OneD((xLen + 1) * (yLen + 1) - 1)
For x As Integer = 0 To xLen
For y As Integer = 0 To yLen
z = y + x * (yLen + 1)
OneD(z) = TwoD(x, y)
Next
Next
PrintValues(OneD, "|")
End Function
Public Sub PrintValues(ByVal myList As IEnumerable, ByVal mySeparator As Char)
' source: https://msdn.microsoft.com/en-us/library/system.collections.arraylist.add(v=vs.110).aspx
Dim obj As [Object]
For Each obj In myList
Console.Write("{0}{1}", mySeparator, obj)
Next obj
Console.WriteLine()
End Sub 'PrintValues
示例: OneD2TwoD(0, 10)
(1) 填充一维数组
|0|1|2|3|4|5|6|7|8|9|10
(2) 将一维数组转换为二维数组
|0|1|2|3|4|5|6|7|8|9|10
(3) 将二维数组转换为一维数组
|0|1|2|3|4|5|6|7|8|9|10
示例: OneD2TwoD(5, 0)
(1) 填充一维数组
|0|1|2|3|4|5
(2) 将一维数组转换为二维数组
|0
|1
|2
|3
|4
|5
(3) 将二维数组转换为一维数组
|0|1|2|3|4|5
示例: OneD2TwoD(2, 5)
(1) 填充一维数组
|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17
(2) 将一维数组转换为二维数组
| 0| 1| 2| 3| 4| 5
| 6| 7| 8| 9|10|11
|12|13|14|15|16|17
(3) 将二维数组转换为一维数组
|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17
感谢您的关注