重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
给一个思路:第一是新建一个数组,把两个数组元素传递过去,这个比较简单;
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都企业网站建设,高端网页制作,对塔吊租赁等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化排名优化,H5建站,响应式网站。
第二:把其中一个数组做为母数组,把另一个数组合并过来,这个用到数组保护机制如下:
Option Explicit
Dim a(): Dim b()
Private Sub Command1_Click() '初始化数组A B
Dim i As Integer
'重新定义数组长度,并赋值
ReDim a(5)
ReDim b(4)
For i = 0 To 5
a(i) = i
Print "数组a" "(" i ")" "=" a(i)
Next i
For i = 0 To 4
b(i) = 1 i
Print "数组b" "(" i ")" "=" b(i)
Next i
End Sub
Private Sub MergeArr_cmd_Click() '合并A 和 B两个数组
Dim s As Integer
Dim h As Integer
Dim i As Integer
h = UBound(a)
s = UBound(a) + UBound(b) + 1
ReDim Preserve a(s) '采用保护机制,把数组A元素加长,长度为:数组A和B的长度之和
For i = 1 To UBound(b) + 1 '将数组B元素加入到数组A中
a(h + i) = b(i - 1)
Next i
For i = 0 To s
Print "数组a" "(" i ")" "=" a(i)
Next
End Sub
Private Sub Command1_Click()
a = Array(1, 2, 4, 7, 9, 34, 65, 73, 78, 99, 234, 1234)
b = Array(0, 23, 33, 56, 78, 98, 99, 234, 1000, 9999)
Dim c(30)
i = 0
j = 0
k = 0
While i = UBound(a) And j = UBound(b)
If a(i) b(j) Then
c(k) = a(i)
i = i + 1
Else
c(k) = b(j)
j = j + 1
End If
k = k + 1
Wend
While i UBound(a)
c(k) = a(i)
i = i + 1: k = k + 1
Wend
While j UBound(b)
c(k) = b(j)
j = j + 1: k = k + 1
Wend
Print "原A数组:"
For i = 0 To UBound(a)
Print a(i);
Next i
Print "原B数组:"
For i = 0 To UBound(b)
Print b(i);
Next i
Print "合并后的c数组:"
For i = 0 To k
Print c(i);
If i Mod 15 = 14 Then Print
Next i
End Sub
Option Explicit
Function JionNumber(a As Variant, b As Variant, c As Variant) 'a数组和b数组 合并到c数组
Dim i As Integer, j As Integer
ReDim c(UBound(a) + UBound(b) + 1)
For i = 0 To UBound(a)
c(i) = a(i)
Next
For j = i To UBound(b) + i
c(j) = b(j - i)
Next
End Function
Private Sub SelectSort(Xarray() As Variant) '选择排序法,升序排序
Dim i%, j%, k%, t%, Start%, Finish%
Start = LBound(Xarray, 1)
Finish = UBound(Xarray, 1)
For i = Start To Finish - 1
k = i
For j = i + 1 To Finish
If Xarray(j) Xarray(k) Then k = j 'k表示当前最大值对应的下标
Next j
If k i Then
t = Xarray(i)
Xarray(i) = Xarray(k)
Xarray(k) = t
End If
Next i
End Sub
Private Sub Command1_Click()
Dim a() As Variant, b() As Variant, c() As Variant, d() As Variant, e() As Variant
Dim f() As Variant '合并后的数组
Dim ab, abc, abcd, i%
a = Array(10, 11, 18, 19, 26, 27, 40, 41, 48, 49)
b = Array(1, 8, 9, 22, 23, 30, 31, 38, 39)
c = Array(6, 7, 14, 15, 28, 29, 36, 37, 44, 45)
d = Array(2, 3, 16, 17, 24, 25, 32, 33, 46, 47)
e = Array(4, 5, 12, 13, 20, 21, 34, 35, 42, 43)
JionNumber a, b, ab '合并数组
JionNumber ab, c, abc
JionNumber abc, d, abcd
JionNumber abcd, e, f
Text1 = "排序前:" vbCrLf
For i = 0 To UBound(f)
Text1 = Text1 " " f(i)
Next i
Text1 = Text1 vbCrLf "排序后:" vbCrLf
SelectSort f() '排序
For i = 0 To UBound(f)
Text1 = Text1 " " f(i)
Next i
End Sub
Text1属性:MultiLine 设为 True,ScrollBars设为2
合并数组没有简单办法,只能一维一维的取出来,放到一新数组中。新数组维数是原两数组维数相加。
比如两个数组 A ,B
dim newAr() '如果两个数组类型相同话 ,可以定义类型。
redim newAr(ubound(A)-lbound(A)+1+ubound(B)-lbound(B)+1-1) ‘由于数组定义只能用常数表达式定义维数,所以用动态数组。
把两数组项目添加到newAr 中
dim Index as integer
dim i as integer
for i=LBound(A) to UBound(A)
newAr(Index)=A(i)
Index=Index+1
next
for i=LBound(B) to UBound(B)
newAr(Index)=B(i)
Index=Index+1
next
Private Sub Command1_Click()
Dim a(1 To 10), b(1 To 10), c(1 To 20)
Randomize
For i = 1 To 10
a(i) = Int(Rnd * 100)
b(i) = Int(Rnd * 100)
Next i
For i = 1 To 9
For j = 10 To i + 1 Step -1
If a(i) a(j) Then d = a(i): a(i) = a(j): a(j) = d
If b(i) b(j) Then d = b(i): b(i) = b(j): b(j) = d
Next j, i
For i = 1 To 10
Print a(i);
Next i
For i = 1 To 10
Print b(i);
Next i
'下面就是合并过程
j = 1
For i = 1 To 10
Do
If a(i) b(j) Then
k = k + 1
c(k) = a(i)
Exit Do
Else
k = k + 1
c(k) = b(j)
j = j + 1
If j 10 Then Exit For
End If
Loop
Next i
If i = 10 Then
For n = i To 10
k = k + 1
c(k) = a(n)
Next n
End If
If j = 10 Then
For n = j To 10
k = k + 1
c(k) = b(n)
Next n
End If
'合并结束
For i = 1 To 20
Print c(i);
Next i
End Sub
Private Sub Form_Load()
Me.AutoRedraw = True
End Sub