Arrays VB.NET从不同数组的元素创建组
我有不同长度和元素的数组。这些数组的数量每次都会不同,但始终会有多个数组 现在,我必须从这些数组的每个元素组合到另一个数组的元素中创建组,并将它们保存在数据库中。简言之,1个组应由每个数组中的1个元素组成 每个数组长度的乘积应该是要生成的组的总数 应该是这样的:Arrays VB.NET从不同数组的元素创建组,arrays,vb.net,dictionary,Arrays,Vb.net,Dictionary,我有不同长度和元素的数组。这些数组的数量每次都会不同,但始终会有多个数组 现在,我必须从这些数组的每个元素组合到另一个数组的元素中创建组,并将它们保存在数据库中。简言之,1个组应由每个数组中的1个元素组成 每个数组长度的乘积应该是要生成的组的总数 应该是这样的: 1ST ARR - [a1, a2, a3, a4] 2ND ARR - [b1, b2, b3] 3RD ARR - [c1, c2, c3] 4TH ARR - [d1, d2, d3, d4, d5
1ST ARR - [a1, a2, a3, a4]
2ND ARR - [b1, b2, b3]
3RD ARR - [c1, c2, c3]
4TH ARR - [d1, d2, d3, d4, d5]
1ST GROUP : a1|b1|c1|d1 (I just used "|" as separator)
2ND GROUP : a2|b1|c1|d1
3RD GROUP : a3|b1|c1|d1
4TH GROUP : a4|b1|c1|d1
5TH GROUP : a1|b2|c1|d1 (index of 2nd array is now incremented)
6TH GROUP : a2|b2|c1|d1
7TH GROUP : a3|b2|c1|d1
8TH GROUP : a4|b2|c1|d1
等等
我尝试将数组放在字典上,并使用整数计数器循环,我成功地循环了第一个数组的元素。但是当涉及到第二个数组时,从我的代码中会发生什么呢?第一个数组和第二个数组恰好同时递增。但是从我对它的理解来看,代码应该在增加第二个数组的索引值之前遍历第一个数组
在我的示例中,我的代码是这样的:
1ST GROUP : a1|b1|c1|d1 (ok)
2ND GROUP : a2|b1|c1|d1 (ok)
3RD GROUP : a3|b1|c1|d1 (ok)
4TH GROUP : a4|b1|c1|d1 (ok)
5TH GROUP : a1|b2|c1|d1 (ok)
6TH GROUP : a2|b3|c1|d1 (should be b2)
这是关于我是如何做到这一点的示例代码:
Dim ls_arr1() As String = {"a1", "a2", "a3", "a4"}
Dim ls_arr2() As String = {"b1", "b2", "b3"}
Dim ls_arr3() As String = {"c1", "c2", "c3"}
Dim ls_arr4() As String = {"d1", "d2", "d3", "d4", "d5"}
Dim l_categories As New Dictionary(Of String, String())
Dim ls_currArr() As String = Nothing
Dim ls_grouped As String = ""
Dim li_grpCount As Integer = 1
Dim n As Integer = 0
Dim lb_Ctr() As Boolean = Nothing
Dim l_indices As New Dictionary(Of String, Integer)
l_categories.Add("ARR1", ls_arr1)
l_categories.Add("ARR2", ls_arr2)
l_categories.Add("ARR3", ls_arr3)
l_categories.Add("ARR4", ls_arr4)
For Each pair As KeyValuePair(Of String, String()) In l_categories
'COUNT NUMBER OF GROUPS THAT MUST BE GENERATED
ls_currArr = pair.Value.ToArray
li_grpCount = li_grpCount * ls_currArr.Count
'THIS WILL BE THE INDEX FOR EVERY ARRAY
l_indices.Add(pair.Key, 0)
Next
For x As Integer = 1 To li_grpCount
For i As Integer = 0 To l_indices.Count - 1
Dim l_pairKey As String = l_indices.ElementAt(i).Key
Dim l_pairVal As Integer = l_indices.ElementAt(i).Value
ls_currArr = l_categories.Item(l_pairKey).ToArray
ls_grouped &= ls_currArr(l_pairVal)
If i < l_indices.Count - 1 Then
ls_grouped &= "|"
End If
'I USED lb_Ctr TO KNOW IF THE INDEX SHOULD BE INCREMENTED
If lb_Ctr(n) = True Then
If (l_indices.Item(l_pairKey) + 1) < l_categories.Count Then
l_indices.Item(l_pairKey) += 1
Else
l_indices.Item(l_pairKey) = 0
lb_Ctr(n + 1) = True
End If
End If
n += 1
Next
'MY SAVE TO DATABASE CODE
ls_grouped = ""
n = 0
Next
Dim ls_arr1()作为字符串={“a1”、“a2”、“a3”、“a4”}
Dim ls_arr2()作为字符串={“b1”、“b2”、“b3”}
Dim ls_arr3()作为字符串={“c1”、“c2”、“c3”}
Dim ls_arr4()作为字符串={“d1”、“d2”、“d3”、“d4”、“d5”}
Dim l_类别作为新字典(字符串的,字符串())
Dim ls_currArr()作为字符串=无
Dim ls_分组为字符串=“”
Dim li_GRP计数为整数=1
尺寸n为整数=0
Dim lb_Ctr()作为布尔值=无
Dim l_索引作为新字典(字符串、整数)
l_类别。添加(“ARR1”,ls_ARR1)
l_类别。添加(“ARR2”,ls_ARR2)
l_类别。添加(“ARR3”,ls_ARR3)
l_类别。添加(“ARR4”,ls_ARR4)
作为l_类别中的KeyValuePair(字符串的字符串())的每一对
'统计必须生成的组数
ls\u currArr=pair.Value.ToArray
li\u grpCount=li\u grpCount*ls\u currArr.Count
'这将是每个数组的索引
l_index.Add(pair.Key,0)
下一个
对于x作为整数=1到li_grpCount
对于i作为整数=0到l_索引。计数-1
Dim l_pairKey As String=l_index.ElementAt(i).Key
Dim l_pairVal作为整数=l_索引.ElementAt(i).Value
l\u currArr=l\u类别。项目(l\u pairKey)。ToArray
ls_分组&=ls_currArr(l_pairVal)
如果i
任何帮助都将不胜感激。即使是不用字典,也没关系。非常感谢。Dim myList作为新列表(数组)
Dim ls_arr1() As String = {"a1", "a2", "a3", "a4"}
Dim ls_arr2() As String = {"b1", "b2", "b3"}
Dim ls_arr3() As String = {"c1", "c2", "c3"}
Dim ls_arr4() As String = {"d1", "d2", "d3", "d4", "d5"}
Dim myList As New List(Of String)
For Each myArr4 In ls_arr4
For Each myArr3 In ls_arr3
For Each myArr2 In ls_arr2
For Each myArr1 In ls_arr1
myList.Add(myArr1 & "|" & myArr2 & "|" & myArr3 & "|" & myArr4)
Next
Next
Next
Next
添加({“a”、“B”、“d”})
添加({“a”、“B”、“d”})
添加({“a”、“B”、“d”})
将myPos(myList.Count)设置为整数
Dim myTotalCnt作为整数=1
对于myCnt=1的myList.Count
myTotalCnt=myTotalCnt*myList(myCnt-1)。长度
下一个
将MyListSlt设置为新列表(字符串)
Dim myIncr为整数=0
对于myCnt=1到myTotalCnt
将myStrList设置为新列表(字符串)
对于myCnt1=1到myList.Count
添加(myList(myCnt1-1)(myPos(myCnt1-1)))
下一个
Dim roro=String.Join(“|”,myStrList)
myListRslt.Add(roro)
myIncr=1
对于myCnt2=1的myList.Count
如果myPos(myCnt2-1)+1
谢谢您的回答,如果我的阵列始终与此相同,我可以这样做。但是在我创建的程序中,不会总是有4个数组,其中包含3个元素或4个元素。有时会有5个或6个不同数组长度的数组。为了便于解释,我只是将其显示为单独的数组变量。无论如何,谢谢:))元素总数不会影响进程,但是对于超过4个数组,您必须区分每个数组,以便可以逐个处理。是的,这正是我的问题。我不能确定将有多少个数组,所以我不能为每个数组使用。你可以这样做:Dim listArray作为新列表(数组):listArray。添加({“a1”、“a2”、“a3”、“a4”)好的。但是如果我把每个数组都放在一个列表中,我仍然看不出如何使用每个数组,因为它只是将当前数组的所有元素循环在一起,然后再移动到下一个数组。你能举个例子吗?
Dim myList As New List(Of Array)
myList.Add({"a", "B", "d"})
myList.Add({"a", "B", "d"})
myList.Add({"a", "B", "d"})
Dim myPos(myList.Count) As Integer
Dim myTotalCnt As Integer = 1
For myCnt = 1 To myList.Count
myTotalCnt = myTotalCnt * myList(myCnt - 1).Length
Next
Dim myListRslt As New List(Of String)
Dim myIncr As Integer = 0
For myCnt = 1 To myTotalCnt
Dim myStrList As New List(Of String)
For myCnt1 = 1 To myList.Count
myStrList.Add(myList(myCnt1 - 1)(myPos(myCnt1 - 1)))
Next
Dim roro = String.Join("|", myStrList)
myListRslt.Add(roro)
myIncr = 1
For myCnt2 = 1 To myList.Count
If myPos(myCnt2 - 1) + 1 < myList(myCnt2 - 1).Length Then
If myIncr = 1 Then
myPos(myCnt2 - 1) += 1
myIncr = 0
End If
Else
myPos(myCnt2 - 1) = 0
myIncr = 1
End If
Debug.Print(myPos(myCnt2 - 1))
Next
Next