在2个数组中的元素之间替换 [英] replacement between element in 2 array
问题描述
如果我有数组1D dim array1()as integer = New Integer(){8,8,1,1,7,7,9,9}和其他数组2D
Dim array2(,)As Integer = New Integer(,){{2,2,2,2,2,2,2},{3,3,3,3,3,3 ,3,3},{4,4,4,4,4,4,4,4},{6,6,6,6,6,6,6,6}}
如何分配array1的元素(分布必须从LSB开始)通过将元素放在array2的LSB中,新数组必须是
array3必须= {{2,2,2,2,2,9,9},{3,3,3,3,3,3,7,7},{4,4,4,4,4,4, 1,1},{6,6,6,6,6,6,8,8}}
if I have array 1D dim array1()as integer =New Integer(){8,8,1,1,7,7,9,9} and other array 2D
Dim array2(,) As Integer = New Integer(,) {{2, 2, 2, 2, 2, 2, 2, 2}, {3, 3, 3, 3, 3, 3, 3, 3}, {4, 4, 4, 4, 4, 4, 4, 4}, {6, 6, 6, 6, 6, 6, 6, 6}}
how can distribute the element of array1 (the distribution must beginning from the LSB) by putting the elements in the LSB of array2 the new array must be
array3 must be ={ {2, 2, 2, 2, 2, 2, 9, 9},{3, 3, 3, 3, 3, 3, 7,7}, {4, 4, 4, 4, 4, 4, 1, 1}, {6, 6, 6, 6, 6, 6, 8, 8}}
推荐答案
你可以从下面的代码中了解
C#
you can get idea from below code
C#
var array1 = "55555555";
var array2 = new [] {"22222222", "33333333", "44444444","66666666"};
var size = array1.Length/array2.Length;
var pos = 0;
for(int i = 0; i < array2.Length; i ++)
{
array2[i] = array2[i] +new String(array1.Skip(pos).Take(size).ToArray());
}
VB
VB
Dim array1 = "55555555"
Dim array2 = New () {"22222222", "33333333", "44444444", "66666666"}
Dim size = array1.Length \ array2.Length
Dim pos = 0
For i As Integer = 0 To array2.Length - 1
array2(i) = array2(i) & New [String](array1.Skip(pos).Take(size).ToArray())
Next
结果
result
2222222255
3333333355
4444444455
6666666655
如果您使用您要在添加项目时重新调整数组大小的数组。我想在这里你最好使用嵌套列表作为2D数据结构。检查下面的示例代码
If you using arrays you need to re size the array when you going to add items. I think here you better use nested list as 2D data structure. Check below sample code
Dim array1() as integer =New Integer(){8,8,1,1,7,7,9,9}
Dim arr2 As New List(Of List(Of Integer))() From { _
New List(Of Integer)() From {2, 2, 2, 2, 2, 2, 2, 2}, _
New List(Of Integer)() From {3, 3, 3, 3, 3, 3, 3, 3}, _
New List(Of Integer)() From {4, 4, 4, 4, 4, 4, 4, 4}, _
New List(Of Integer)() From {6, 6, 6, 6, 6, 6, 6, 6}}
Dim size =array1.Length/(arr2.Count)
Dim pos As Integer = 0
For i As Integer = 0 To arr2.Count -1
pos =i*size
arr2(i).AddRange(array1.Skip(pos).Take(size).ToArray())
Next
这篇关于在2个数组中的元素之间替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!