.NET 2.0中是否有一个内置函数,可以将两个数组合并成一个数组?
这两个数组具有相同的类型。我从代码库中广泛使用的函数中获得这些数组,并且不能修改该函数以以不同的格式返回数据。
如果可能的话,我希望避免编写自己的函数来完成这个任务。
.NET 2.0中是否有一个内置函数,可以将两个数组合并成一个数组?
这两个数组具有相同的类型。我从代码库中广泛使用的函数中获得这些数组,并且不能修改该函数以以不同的格式返回数据。
如果可能的话,我希望避免编写自己的函数来完成这个任务。
当前回答
假设目标数组有足够的空间,array . copy()将工作。您也可以尝试使用List<T>及其. addrange()方法。
其他回答
我认为你可以使用数组。收到。它有一个源索引和一个目标索引,因此您应该能够将一个数组附加到另一个数组。如果您需要更复杂的操作,而不仅仅是将一个附加到另一个,那么这个工具可能不适合您。
我需要一个解决方案来组合未知数量的数组。
令人惊讶的是没有人提供一个解决方案使用SelectMany与参数。
private static T[] Combine<T>(params IEnumerable<T>[] items) =>
items.SelectMany(i => i).Distinct().ToArray();
如果你不想要不同的项目,只需删除不同的项目。
public string[] Reds = new [] { "Red", "Crimson", "TrafficLightRed" };
public string[] Greens = new [] { "Green", "LimeGreen" };
public string[] Blues = new [] { "Blue", "SkyBlue", "Navy" };
public string[] Colors = Combine(Reds, Greens, Blues);
注意:使用distinct时,绝对不能保证顺序。
我想找到一种不使用数组本身以外的任何库或功能的方法。
前两个示例主要用于从头读取逻辑,但我也想知道是否会根据情况而有性能变化。
第三个例子是最实际的选择。
// Two for-loops
private static int[] MergedArrays_1(int[] a, int[] b)
{
int[] result = new int[a.Length + b.Length];
for (int i = 0; i < a.Length; i++)
{
result[i] = a[i];
}
for (int i = a.Length; i < result.Length; i++)
{
result[i] = b[i - a.Length];
}
return result;
}
// One for-loop
private static int[] MergedArrays_2(int[] a, int[] b)
{
int[] results = new int[a.Length + b.Length];
for (int i = 0; i < results.Length; i++)
{
results[i] = (i < a.Length) ? a[i] : b[i - a.Length];
}
return results;
}
// Array Method
private static int[] MergedArrays_3(int[] a, int[] b)
{
int[] results = new int[a.Length + b.Length];
a.CopyTo(results, 0);
b.CopyTo(results, a.Length);
return results;
}
最后,我做了第四个例子,可以合并多个数组,使用params关键字。
int[] result = MultipleMergedArrays(arrayOne, arrayTwo, arrayThree);
private static int[] MultipleMergedArrays(params int[][] a)
{
// Get Length
int resultsLength = 0;
for (int row = 0; row < a.GetLength(0); row++)
{
resultsLength += a.Length;
}
// Initialize
int[] results = new int[resultsLength];
// Add Items
int index = 0;
for (int row = 0; row < a.GetLength(0); row++)
{
a[row].CopyTo(results, index);
index += a[row].Length;
}
return results;
}
当使用参数时,它的工作方式是将一维数组传递到锯齿数组中。
GetLength(0)返回锯齿状数组中包含的数组数。
该代码首先计算所有数组的Length,然后根据该大小初始化一个新数组,并开始使用CopyTo()方法将整个数组添加到新的结果数组中,同时将每个添加的数组的Length添加到索引计数器中。
PS:在合并时,有时需要从数组中删除空项或某些项。
private static int[] RemoveEmpty(int[] array)
{
int count = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 0) count++;
}
int[] result = new int[array.Length - count];
count = 0;
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 0) continue;
result[count] = array[i];
count++;
}
return result;
}
这个函数可以与上面的函数结合使用。
它接受一个数组,计算匹配为0的项的数量。并创建一个大小合适的新数组。然后计数器被回收并用作索引,用于将输入数组的值放入新的更小的结果数组中。 当一个项匹配0时,它将跳过该循环中的其余代码,并继续下一轮循环,不增加整数计数器。
只是有一个选项:如果你正在使用的数组是一个基本类型-布尔(bool), Char, SByte, Byte, Int16(短),UInt16, Int32 (int), UInt32, Int64(长),UInt64, IntPtr, UIntPtr,单,或双-那么你可以(或应该?)尝试使用Buffer.BlockCopy。根据Buffer类的MSDN页面:
与系统中的类似方法相比,这个类在操作基元类型方面提供了更好的性能。数组类。
使用@OwenP回答中的c# 2.0示例作为起点,它将如下所示:
int[] front = { 1, 2, 3, 4 };
int[] back = { 5, 6, 7, 8 };
int[] combined = new int[front.Length + back.Length];
Buffer.BlockCopy(front, 0, combined, 0, front.Length);
Buffer.BlockCopy(back, 0, combined, front.Length, back.Length);
Buffer之间在语法上几乎没有任何区别。BlockCopy和Array。复制@OwenP使用的,但这应该更快(即使只有一点点)。
假设目标数组有足够的空间,array . copy()将工作。您也可以尝试使用List<T>及其. addrange()方法。