vb数组问题:怎么对比多个数组并统计不同的个数

2025-03-03 18:05:08
推荐回答(4个)
回答1:

假如数组为a(6,6)

dim i as integer
dim j as integer
dim k as integer
dim iCount as integer
dim bFlag as boolean
dim b(6) as integer '用于存放已经统计过的相同数组
iCount = 6 '不同数组个数
for i = 1 to 6
b(i) = 0
next i

for i = 1 to 6
if b(i) = 1 then continue
for j = i to 6
if b(j) = 1 then continue
bFlag = false
for k = 1 to 6
if a(i,k) <> a(j,k) then
break
bFlag = true
end if
if bFlag = false then '相同
b(j) = 1
iCount = iCount -1
end if
next k
next j
b(i) = 1
next i

print "不同数组个数为:" & iCount & " 个"

回答2:

Dim x%, y%, z%, C As Boolean
'例如数组名称为SN
z = 1
For x = 1 To UBound(SN) - 1
C = False
For y = x + 1 To UBound(SN)
If SN(x) = SN(y) Then C = True: Exit For
Next
If Not C Then z = z + 1
Next
MsgBox "共有" & z & "个不同数组!"

回答3:

Dim g(5) As Integer

g(0) = 123456
g(1) = 124255
g(2) = 123456
g(3) = 326231
g(4) = 512214
g(5) = 123456

For i = 0 To Ubound(g)
For j = 0 To Ubound(g)
If g(i) = g(j) And i <> j And g(i) <> -1 Then
g(i) = -1
End If
Next j
Next i

Dim c As Integer
For i = 0 To Ubound(g)
If g(i) <> -1 Then
c = c + 1
End If
Next i

Msgbox "不同的数组个数为" & c & "个"

回答4:

C#的语法。

int [] a = new int []{123456,
124255,
123456,
326231,
512214,
123456};

List lst = new List();

foreach(int i in a)
{
if (!lst.Contain(i))
{
lst.Add(i);
}
}

Console.WriteLine("不同的数量有{0}个.", lst.Count);