本文介绍了如何将整数值移动到两个不同的新数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好, 我有五(5)个不同的数组,其整数值如下: Dim group1 作为 整数 = { 10 , 12 , 21 } Dim group2 作为 整数 = { 31 , 32 , 33 } Dim group3 作为 整数 = { 11 , 12 , 13 } Dim group4 作为 整数 = { 43 , 51 } Dim group5 正如 整数 = { 11 , 65 } 现在,上述值对于与一个人相关的每条记录都是动态的,但我的数据库包含数千人。 规则: 1.在第一个group1数组中;我将所有值传递给另一个新的数组,称之为CombinedArray,所以我将{10,12,21}。 2.在第二个group2数组中,我只将前两个最高值移动到同一个CombinedArray,所以我现在将{10,12,21,33,32},最低值{31}移动到另一个新数组调用它AllArrays {31} 3.在第三个group3数组中,我排序并只取一个最高数组并将其移动到CombinedArray,使其变为{10,12,21 ,33,32,13}。此数组中的其余值将移至AllArrays {31,11,12} 4.接下来,我将group4数组和group5数组中的所有值移至AllArrays,以便它们现在显示为{31 ,11,12,43,51,11,65}。对此组应用排序,仅取1个最高值; (65)并将其移至CombinedArray以获得{10,12,21,33,32,13,65}。 5.循环并总结此CombinedArray的值。就这些。 我是VB和数组的新手。任何人都可以提供帮助。 谢谢。 我尝试了什么: 我可以按照下面的顺序排序; 按降序排序数组: 私有 Sub Button8_Click(发件人 As 对象,e As EventArgs)句柄 Button8.Click Dim array()作为 整数 = { 3 , 5 , 200 , 1 } Dim maxPos As 整数 Dim firstI As 整数 而 firstI< = UBound(array) 对于 i = firstI 到 UBound(数组) 如果数组(i)> array(maxPos)然后 maxPos = i 结束 如果 下一步 Dim largestNumber As Integer = array(maxPos) array(maxPos)= array(firstI)数组(firstI)= maximumNumber firstI = firstI + 1 maxPos = firstI ' Console.WriteLine(largestNumber) MessageBox.Show(largestNumber) ListBoxNumbers.Items.Add(largestNumber) 结束 结束 Sub 解决方案 为了能够增加数组的大小而不丢失原始值,你必须调用 Redim保留ArrayName(NewSize)。请参阅: Visual Basic中的数组| Microsoft Docs [ ^ ] 要按升序排序整数数组,请使用Array.Sort [ ^ ] 要排序整数数组按降序排列,使用 Array.Sort 然后 Array.Reverse [ ^ ]。 这是一小段代码,你可以用来复制 groupX 数组和CombinedArray和AllArrays之间的数据: Dim CopyNoOfElements = Sub ( ByRef SrcList AS 整数(), ByVal NoOfEle 作为 整数, ByRef DstList AS 整数()) Dim j As Integer = DstList.Length + NoOfEle -1 Redim 保留DstList(j) 对于 i As 整数 = 1 NoOfEle DstList(j + i-NoOfEle)= SrcList(i-1) 下一步 结束 Sub Dim CombinedArray As Integer ()= 新 整数(){} Dim AllArrays 作为 整数()= 新 整数(){} ' group1 Console.WriteLine( 处理组1:({0} ),字符串 .Join( ;,group1)) CopyNoOfElements(group1,group1.Length,CombinedArray) Console.WriteLine( CombinedArray:({0}), String .Join( ;,CombinedArray)) Console.WriteLine( AllArrays:({0}), String .Join( ;,AllArrays)) Console.WriteLine() group2 Console.WriteLine( 处理group2:({0}),字符串 .Join( ;,group2)) Array.Sort(group2) Array.Reverse(group2) CopyNoOfElements(group2, 2 ,CombinedArray) Console.WriteLine( CombinedArray:({0}),字符串 .Join( ;,CombinedArray)) Array.Sort(group2) CopyNoOfElements(group2,group2.Length-2,AllArrays)控制台。 WriteLine( AllArrays:({0}),字符串 .Join( ;,AllArrays)) Console.WriteLine () ;) 最后的注释: 你真的需要在阵列上工作吗?您应该处理类和类列表。这就是我们所说的: OOP的力量 [ ^ ]! Hello,I have five (5) different arrays with integer values as follows:Dim group1 As Integer = {10, 12, 21}Dim group2 As Integer = {31, 32, 33}Dim group3 As Integer = {11, 12, 13}Dim group4 As Integer = {43, 51}Dim group5 As Integer = {11, 65}Now, the above values are dynamic for each record related to one person but my database contains thousands of people.Rules:1. In the first group1 array; I transfer all the values to a different new array call it CombinedArray, so I will have {10, 12, 21}.2. In the second group2 array, I move only first two highest values to the same CombinedArray so I will now have {10, 12, 21, 33, 32}, the lowest value {31} is moved to another new array call it AllArrays {31}3. In third group3 array, I sort and take only 1 highest array and move it to CombinedArray so that it becomes {10, 12, 21, 33, 32, 13}. The rest of values from this array is moved to AllArrays {31, 11, 12}4. Next, I move all values in group4 array and group5 array to AllArrays so that they now appear as {31, 11, 12, 43, 51, 11, 65}. Apply a sort on this group and take only 1 highest value; (65) and move it to CombinedArray to get {10, 12, 21, 33, 32, 13, 65}.5. Loop through and sum the values of this CombinedArray. That is all.I am new to VB and arrays. Can any one out there help.Thanks.What I have tried:I am able to sort descending on this like below;Sort array in descending order:Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click Dim array() As Integer = {3, 5, 200, 1} Dim maxPos As Integer Dim firstI As Integer While firstI <= UBound(array) For i = firstI To UBound(array) If array(i) > array(maxPos) Then maxPos = i End If Next Dim largestNumber As Integer = array(maxPos) array(maxPos) = array(firstI) array(firstI) = largestNumber firstI = firstI + 1 maxPos = firstI 'Console.WriteLine(largestNumber) MessageBox.Show(largestNumber) ListBoxNumbers.Items.Add(largestNumber) End While End Sub 解决方案 To be able to increase the size of array, without loosing original values, you have to call Redim Preserve ArrayName(NewSize). See: Arrays in Visual Basic | Microsoft Docs[^]To sort array of integers in ascending order, use Array.Sort[^]To sort array of integers in descending order, use Array.Sort and then Array.Reverse[^].Here is small piece of code, which you may use to copy data between groupX array and CombinedArray and AllArrays:Dim CopyNoOfElements = Sub(ByRef SrcList AS Integer(), ByVal NoOfEle As Integer, ByRef DstList AS Integer()) Dim j As Integer = DstList.Length + NoOfEle -1 Redim Preserve DstList(j) For i As Integer = 1 To NoOfEle DstList(j+i-NoOfEle) = SrcList(i-1) Next End SubDim CombinedArray As Integer() = New Integer(){}Dim AllArrays As Integer() = New Integer(){}'group1Console.WriteLine("Processing group1: ({0})" ,String.Join(";", group1))CopyNoOfElements(group1, group1.Length, CombinedArray)Console.WriteLine("CombinedArray: ({0})" ,String.Join(";", CombinedArray))Console.WriteLine("AllArrays: ({0})" ,String.Join(";", AllArrays))Console.WriteLine()'group2Console.WriteLine("Processing group2: ({0})" ,String.Join(";", group2))Array.Sort(group2)Array.Reverse(group2)CopyNoOfElements(group2, 2, CombinedArray)Console.WriteLine("CombinedArray: ({0})" ,String.Join(";", CombinedArray))Array.Sort(group2)CopyNoOfElements(group2, group2.Length-2, AllArrays)Console.WriteLine("AllArrays: ({0})" ,String.Join(";", AllArrays))Console.WriteLine();)Final note:Do you really need to work on arrays? You should work on classes and list of classes. This is what we call: The power of OOP[^]! 这篇关于如何将整数值移动到两个不同的新数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 18:01