比如int[] a={1,2,3,4},int[] b={5,6,7,8}
合并成数组{1,2,3,4,5,6,7,8}
========================
还有有数组{1,2,3,8,4,2,9,3,4,5,8,3,4,9,3}如何提取里面8开头、9结尾的小数组形成{8,4,2,9}和{8,3,4,9}??
谢谢
合并成数组{1,2,3,4,5,6,7,8}
========================
还有有数组{1,2,3,8,4,2,9,3,4,5,8,3,4,9,3}如何提取里面8开头、9结尾的小数组形成{8,4,2,9}和{8,3,4,9}??
谢谢
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 5, 6, 7, 8 };
public void Test()
{
int[] t = a.Union(b).ToArray();
foreach (int z in t)
{
System.Console.Out.WriteLine(z.ToString());
}
}
static void Main(string[] args)
{
Program p = new Program();
p.Test();
}
}
{ List<List<int>> tmp = new List<List<int>>();
List<int> lst = null;
bool bFind = false;
foreach (int z in c)
{
if (z == 8)
{
lst = new List<int>();
tmp.Add(lst);
bFind = true;
}
if (bFind)
{
lst.Add(z);
if (z == 9)
bFind = false;
}
}
int[] u1 = tmp[0].ToArray();
int[] u2 = tmp[1].ToArray(); foreach (int z in u1)
{
System.Console.Out.WriteLine(z.ToString());
}
foreach (int z in u2)
{
System.Console.Out.WriteLine(z.ToString());
}
}
static void Main(string[] args)
{
Program p = new Program();
p.Test2();
}
不知楼主满意不。
{
List<int> resultList = new List<int>();
int firstIndex = 0;
int secondIndex = 0;
while (firstIndex < targetArray1.Length && secondIndex < targetArray2.Length)
{
if (targetArray1[firstIndex] < targetArray2[secondIndex])
{
resultList.Add(targetArray1[firstIndex]);
firstIndex++;
}
else if (targetArray1[firstIndex] == targetArray2[secondIndex])
{
resultList.Add(targetArray1[firstIndex]);
resultList.Add(targetArray2[secondIndex]);
firstIndex++;
secondIndex++;
}
else
{
resultList.Add(targetArray2[secondIndex]);
secondIndex++;
}
}
if (firstIndex >= targetArray1.Length && secondIndex < targetArray2.Length)
{
for (int i = secondIndex; i < targetArray2.Length; i++)
{
resultList.Add(targetArray2[i]);
}
}
else if (secondIndex >= targetArray2.Length && firstIndex < targetArray1.Length)
{
for (int i = firstIndex; i < targetArray1.Length; i++)
{
resultList.Add(targetArray1[i]);
}
}
Console.WriteLine("组合后的数组为:");
foreach (int item in resultList)
{
Console.Write(item + " ");
}
Console.WriteLine();
Console.WriteLine("测试成功!");
}
#endregion
合并2个递增数组
第二题正在处理
int[] b = { 5, 6, 7, 8 };
int[] c={1,2,3,8,4,2,9,3,4,5,8,3,4,9,3};
public void Test()
{
// int[] t = a.Union(b).ToArray();
int[] t = a.Concat(b).ToArray();
foreach (int z in t)
{
System.Console.Out.WriteLine(z.ToString());
}
}
Union方法从结果集中排除重复项。这是与 Concat<TSource> 方法不同的行为,后者返回输入序列中的所有元素,包括重复项。
默认相等比较器 Default 用于比较实现了 IEqualityComparer<T> 泛型接口的类型的值。若要比较自定义类型,需要为该类型实现此接口并提供自己的 GetHashCode 和 Equals 方法。
当枚举此方法返回的对象时,Union 会按顺序枚举 first 和 second,并生成尚未生成的每个元素。
希望楼主看看LINQ帮助。
{
int[] a = { 1, 2, 3, 4 };
int[] b = { 5, 6, 7, 8 };
int[] c = { 1, 2, 3, 8, 4, 2, 9, 3, 4, 5, 8, 3, 4, 9, 3 };
int start = c.ToList().FindIndex(x => x == 8);
int end = c.ToList().FindIndex(x => x == 9);
int[] arr1=new int[end-start+1];
c.ToList().CopyTo(start, arr1, 0, end - start + 1);
start = c.ToList().FindIndex(end + 1, x => x == 8);
end = c.ToList().FindIndex(end + 1,x => x == 9);
int[] arr2 = new int[end - start + 1];
c.ToList().CopyTo(start, arr2, 0, end - start + 1); foreach (int z in arr1)
{
System.Console.Out.WriteLine(z.ToString());
}
foreach (int z in arr2)
{
System.Console.Out.WriteLine(z.ToString());
}
}
楼主这样也能把两个数据提取出来,这个更好理解。