今天的学习遇到个问题就是二维数组转为一维数组。通常情况下使用两次循环来实现:
构建的二维数组:
Dim a2(,) As Byte = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}
转为一维数组:
Dim b1(a2.Length - 1) As Byte
Dim length1 As Integer
length1 = a2.GetLength(0)
Dim length2 As Integer
length2 = a2.GetLength(1)
For i As Integer = 0 To a2.GetLength(0) - 1
For j As Integer = 0 To a2.GetLength(1) - 1
b1(i * length2 + j) = a2(i, j)
Next
Next
实际还有一种方法,使用一次循环就可以达到效果:
Dim c1(a2.Length - 1) As Byte
Dim index As Integer = -0
For Each a As Byte In a2
c1(index) = a
index += 1
Next
做好记录备查。