VB如何统计二维数组中相同元素的个数?

问题描述:

VB如何统计二维数组中相同元素的个数?
比如:
A(1,1)=2
A(1,2)=3a
A(2,1)=11
A(2,2)=5
A(3,1)=34
A(3,2)=11t
A(4,1)=55
A(4,2)=3a
A(5,1)=57
A(5,2)=11t
A(6,1)=72
A(6,2)=3a
统计后形成(注意按b(i,1)的大小升序排列)
B(1,1)=3a
B(1,2)=3个
B(2,1)=5
B(2,2)=1个
B(3,1)=11t
B(3,2)=2个

Private Sub Command1_Click()
Dim a(),b(),blnFlag As Boolean,F%
Dim i%,j%,k%
ReDim a(1 To 6,1 To 2)
a(1,1) = "2"
a(1,2) = "3a"
a(2,1) = "11"
a(2,2) = "5"
a(3,1) = "34"
a(3,2) = "11t"
a(4,1) = "55"
a(4,2) = "3a"
a(5,1) = "57"
a(5,2) = "11t"
a(6,1) = "72"
a(6,2) = "3a"
ReDim b(1 To 2,1 To 1) ' B(1,1)=3a B(2,1)=3个
b(1,UBound(b,2)) = "" ' a(LBound(a,1),LBound(a,2))
For i = LBound(a,1) To UBound(a,1)
For j = LBound(a,2) To UBound(a,2)
blnFlag = False
For k = LBound(b,2) To UBound(b,2)
If a(i,j) = b(1,k) Then blnFlag = True:Exit For
Next
If blnFlag Then
b(2,k) = b(2,k) + 1
Else
b(1,UBound(b,2)) = a(i,j)
b(2,UBound(b,2)) = 1
ReDim Preserve b(1 To 2,LBound(b,2) To UBound(b,2) + 1) '保留原值重定义数组
b(1,UBound(b,2)) = ""
b(2,UBound(b,2)) = 0
End If
Next
Next
For k = LBound(b,2) To UBound(b,2)
Print b(1,k),b(2,k)
Next
End Sub
'b 数组中二维下标的位置换了一下,以便于 保留原值重定义数组 而不出错!