如何合并数组数据? [英] How do I union array data?

查看:60
本文介绍了如何合并数组数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我.
40 + 5-3; 5 + 10-2是第一个数组数据.

42 | 13是第二个数组数据.

第二个数组数据42是第一个数组数据40 + 5-3的计算结果,然后是

第二数组数据13是第一数组数据5 + 10-3的计算结果.

我希望在第三个数组中低于结果
42 | 40 | 5 | 3 | 13 | 5 | 10 | 2
---------------
在第三个数组中
例如:
计算结果报表|计算结果|声明
--------------
第三个数组是第一个数组和第二个数组的并集.

请回复我.

谢谢..

Pls help me.
40+5-3;5+10-2 is the fist array data.

42|13 is second array data.

Second array data 42 is first array data 40+5-3 calculation result and then

second array data 13 is first array data 5+10-3 calculation result.

I wish to be below result in third array
42|40|5|3|13|5|10|2
---------------
In third array
eg:
Calculation result | Statement|Calculation Result | Statement
--------------
Third array is union of first and second array.

Pls reply me.

Thanks..

推荐答案

Union方法排除此处给出的重复项
http://msdn.microsoft.com/en-us/library/bb341731.aspx#Y100 [^ ]
因此,如果有一条语句"35+10-3"给出结果"42",则该语句将不包括在语句和结果的并集中.可以使用Concat 方法.如果使用,则首先列出第一个数组的所有元素,然后列出第二个数组的元素.但是,正如问题中给出的那样,需要result, statement顺序.在这种情况下,我认为可以使用以下代码更好地控制元素的顺序
The Union method excludes the duplicates as given here
http://msdn.microsoft.com/en-us/library/bb341731.aspx#Y100[^]
So, if there is a statement say "35+10-3" which gives the result "42", then this will not be included in the union of statements and results. Concat method can be used. If it is used all the elements from first array are listed first and then the elements of the second array are listed. But as given in the question, result, statement order is required. In which case I think the following code can be used for better control on the order of the elements
Dim statements As String() = {"40+5-3", "5+10-2", "35+10-3"}
Dim results As String() = {"42", "13", "42"}
'Dim resultsStatements As String() = New String(Math.Min(statements.Length, results.Length) * 2 - 1) {}
'Ensure that the statements and results are of same length
If statements.Length <> results.Length Then
	Console.WriteLine("The number of Statements and number of Results should be equal")
	Return
End if
Dim resultsStatements As String() = New String(statements.Length * 2 - 1) {}
Dim i As Integer = 0
'While i < statements.Length AndAlso i < results.Length
While i < statements.Length 
	resultsStatements(i * 2) = results(i)
	resultsStatements(i * 2 + 1) = statements(i)
	i += 1
End While
For Each element As String In resultsStatements
	Console.WriteLine(element)
Next
'Output
'42
'40+5-3
'13
'5+10-2
'42
'35+10-3


使用 Enumerable.Union [

这篇关于如何合并数组数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆