如何在VB中随机排列数组? [英] how to shuffle array in VB?
问题描述
我正在尝试创建一个应用程序,它将打乱一个字符串数组并生成 2 个完全不同的版本,其中没有元素相互匹配例如,初始数组是 A、B、C、D、E,而混洗后的数组必须是 B、D、E、A、C.
I'm trying to create and application which will shuffle an string array and produce 2 totally different versions where no element will match each other like for example the initial array is A, B, C, D, E than the shuffled array must be B, D, E, A, C.
在我的情况下,当我对它们进行混洗并尝试产生输出时,我会得到混洗的数组,但它们彼此完全相同.似乎最后一个数组中的值覆盖了前一个数组中的值.
In my case when I suffle them and try to produce an output I get shuffled array but they are completely identical to each other. It seems like the values in last array override the values of the previous ones.
我试图保护他们,但我不知道该怎么做.请任何人都可以告诉我我做错了什么吗?
I tried to protect them but I don't know how to do it. Please can anybody give me a hint about what am I doing wrong?
Dim myArray() As String = {"A", "B", "C", "D", "E"}
这是触发shuffle的按钮代码
This is the code of the button which triggers shuffle
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
RendomOutput1(myArray)
End Sub
结束课程
这是打乱第一个数组的函数:
THis is the function that shuffles first array:
Sub RendomOutput1(ByVal x() As String)
Dim t As Integer
Dim Temp As String
For i As Integer = 0 To x.Count - 1
Dim rnd As New Random
t = rnd.Next(0, x.Count - 1)
Temp = x(t)
x(t) = x(i)
x(i) = Temp
Next
RandomOutput2(x)
End Sub
这是产生另一个数组并打印结果的函数:
This is the function which produces another array and prints the result:
Sub RendomOutput2(ByRef y() As String)
Dim y1() As String = y' May be I shall lock or somehow protect y1 here?
'Lock(y1) doesn't work
Dim t As Integer
Dim Temp As String
For i As Integer = 0 To y.Count - 1
Dim rnd As New Random
t = rnd.Next(0, y.Count - 1)
Temp = y(t)
y(t) = y(i)
y(i) = Temp
Next
For i As Integer = 0 To x.Count - 1
Label1.Text &= y1(i) & " IS NOT " & y(i) & vbCrLf
Next
End Sub
结果数组 y1 和 y 与初始值不同,但彼此相同.有谁知道我怎样才能让他们与众不同.可能锁定 y1 数组或其他东西.提前致谢
IN the result arrays y1 and y are different from initial but identical to each other. Does anybody know how can I make them different. Probably lock y1 array or something. Thank you in advance
推荐答案
此行
Dim y1() As String = y
不创建新数组 - 它创建对现有数组的引用.因此,您将有两个数组引用(y
和 y1
),但只有一个数组(两个引用都指向同一个数组).当您对 y
进行更改时,这些更改通过 y1
可见,因为它们都引用相同的底层数组.
doesn't create a new array - it creates a reference to an existing array. So you'll have two array references (y
and y1
) but only one array (both references point to the same array). When you make changes to y
those changes are visible through y1
because both of them refer to the same underlying array.
您需要的是 2 个不同的数组实例,其中保存的数据是重复的(也就是说,您需要 2 个指向 2 个不同数组的数组引用).那么对一个数组所做的更改不会影响另一个数组.
What you need is 2 distinct array instances where the data held be the arrays are duplicated (that is, you need 2 array references that point to 2 different arrays). Then changes made to one array will not affect the other array.
例如:
' Create new array from the input array
Dim y1() As String = new String(y.Count-1){}
For i As Integer = 0 To y.Count-1
y1(i) = y(i)
Next i
或者,您也可以克隆数组:
Dim y1() As String = y.Clone()
在幕后,这会导致同样的事情.
Behind the scenes this results in the same thing.
这篇关于如何在VB中随机排列数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!