COPYMEMORY 实现 N维数组向M维数组的转化
'copymemory可以实现不同维数数组间的转换,如:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Dim a(5, 7) As Byte, b(15, 2) As Byte, c(47) As Byte, d(2, 3, 3) As Byte '定义48个元素的四个数组用于测试
Sub xxxx()
Dim i As Integer, j As Integer, k As Integer
For i = 0 To 5 '随机定义数组a
For j = 0 To 7
a(i, j) = Int(Rnd * 128)
Next
Next
Debug.Print "array a():" '数组a( 6 行8 列)
For i = 0 To 5
For j = 0 To 7
Debug.Print Right(Space(5) & a(i, j), 5);
Next
Debug.Print
Next
Debug.Print
Debug.Print "array b():" '数组b( 16 行 3 列)
CopyMemory b(0, 0), a(0, 0), 48
For i = 0 To 15
For j = 0 To 2
Debug.Print Right(Space(5) & b(i, j), 5);
Next
Debug.Print
Next
Debug.Print
Debug.Print "array c():" '数组c( 48 行 1 列)
CopyMemory c(0), b(0, 0), 48
For i = 0 To 47
Debug.Print Right(Space(5) & c(i), 5)
Next
Debug.Print
Debug.Print "array d():" '数组d( 3 行 4 列 4 层)
CopyMemory d(0, 0, 0), c(0), 48
For i = 0 To 2
For j = 0 To 3
For k = 0 To 3
Debug.Print Right(Space(5) & d(i, j, k), 5);
Next
Debug.Print
Next
Debug.Print "---------------------------"
Next
End Sub
返回:
array a():
31 125 7 49 46 62 19 60
32 80 69 20 120 83 64 49
13 100 58 96 76 106 2 26
9 13 42 16 0 68 84 69
105 10 24 86 58 45 19 90
118 67 11 96 51 59 62 26
array b():
31 24 106
32 11 68
13 49 45
9 20 59
105 96 19
118 16 64
125 86 2
80 96 84
100 46 19
13 120 62
10 76 60
67 0 49
7 58 26
69 51 69
58 62 90
42 83 26
array c():
31
32
13
9
105
118
125
80
100
13
10
67
7
69
58
42
24
11
49
20
96
16
86
96
46
120
76
0
58
51
62
83
106
68
45
59
19
64
2
84
19
62
60
49
26
69
90
26
array d():
31 7 46 19
9 42 0 84
125 49 62 60
13 16 68 69
---------------------------
32 69 120 64
105 24 58 19
80 20 83 49
10 86 45 90
---------------------------
13 58 76 2
118 11 51 62
100 96 106 26
67 96 59 26
---------------------------