dim xxx
dim Temp
xxx = Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)Temp = Array(3,6,14,1,5,18)Temp数组中的元素是随机的,但范围在1-20间,也就是XXX的子集要求xxx删除temp中的元素,得到新的一个数组···怎样的算法最好?我用算法比较差,用了 词典 dim xxx As New Dictionary ,添加修改都非常方便。
但人家都说数组存储快,上面20个元素只是举例子,事实不止20个···
dim Temp
xxx = Array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)Temp = Array(3,6,14,1,5,18)Temp数组中的元素是随机的,但范围在1-20间,也就是XXX的子集要求xxx删除temp中的元素,得到新的一个数组···怎样的算法最好?我用算法比较差,用了 词典 dim xxx As New Dictionary ,添加修改都非常方便。
但人家都说数组存储快,上面20个元素只是举例子,事实不止20个···
Dim xxx
Dim Temp
Dim result()
xxx = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)Temp = Array(3, 6, 14, 1, 5, 18)Dim i, j, count
Dim isDelete As Booleancount = 0 '用来计数
For i = 0 To UBound(xxx)
isDelete = False
For j = 0 To UBound(Temp)
If Temp(j) = xxx(i) Then '让temp中的数据与xxx的对比是否存在
isDelete = True
Exit For
End If
Next j
'如果没有在temp中找到数值,则将其加入新数组
If Not isDelete Then
ReDim Preserve result(count)
result(count) = xxx(i)
count = count + 1
End If
Next i'遍历结果
For i = 0 To UBound(result)
Debug.Print result(i)
Next i
xxx = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
Temp = Array(3, 6, 14, 1, 5, 18)
dim newxxx() as integer
for i = 0 to ubound(Temp)
xxx(Temp(i)-1)= 0
nextredim newxxx(20-ubound(temp)) as integer
dim Count as integer
for i = 0 to 19
if xxx(i)<> 0 then
newxxx(count) = xxx(i)
count++
end if
next