写一个函数:
Private Sub Command1_Click()
Dim x(1 To 50) As String, i As Long
For i = 1 To 50
x(i) = i * i
Next
Dim y
y = copyarray(x)
MsgBox Join(y, ",")
End Sub
Function copyarray(ByRef a() As String)
Dim b() As String
Dim i As Long, temp As New Collection, tempnum As Long
For i = LBound(a) To UBound(a)
temp.Add i
Next
Randomize
ReDim b(LBound(a) To UBound(a))
For i = LBound(a) To UBound(a)
tempnum = Int(temp.Count * Rnd + 1)
b(i) = a(temp(tempnum))
temp.Remove tempnum
Next
copyarray = b
Erase b
End Function
'一个按钮,两个listbox(用于显示数组的元素)
'要加入对Microsoft Scripting Runtime的引用
Option Explicit
Dim a() As Long
Dim b() As Long
Private Sub Command1_Click()
List2.Clear
Dim dic As New Dictionary
Dim i As Long
Dim k As Long
ReDim b(50)
For i = 0 To 50
Randomize Timer
k = CLng(Rnd() * 50) + 0
Do While dic.Exists(k) = True
Randomize Timer
k = CLng(Rnd() * 50) + 0
Loop
dic.Add k, k + 1
b(i) = a(k)
List2.AddItem CStr(b(i))
Next
Set dic = Nothing
End Sub
Private Sub Form_Load()
'初始化数组a
ReDim a(50)
Dim i As Long
For i = 0 To 50
a(i) = 2 * i + 1
List1.AddItem CStr(a(i))
Next
End Sub
Dim j as Integer,k as Integer,Numsel as Integer
For k = 1 To N
Randomize Timer
NumSel = Int(Rnd() * (N + 2 - k))
b(k) = a(NumSel)
If NumSel = 1 Then
For j = 1 To N - k
a(j) = a(j + 1)
Next
ElseIf NumSel > 1 And NumSel <> N + 1 - k Then
For j = NumSel To N - k
a(j) = a(j + 1)
Next
End If
Next