.NET 2.0中是否有一个内置函数,可以将两个数组合并成一个数组?
这两个数组具有相同的类型。我从代码库中广泛使用的函数中获得这些数组,并且不能修改该函数以以不同的格式返回数据。
如果可能的话,我希望避免编写自己的函数来完成这个任务。
.NET 2.0中是否有一个内置函数,可以将两个数组合并成一个数组?
这两个数组具有相同的类型。我从代码库中广泛使用的函数中获得这些数组,并且不能修改该函数以以不同的格式返回数据。
如果可能的话,我希望避免编写自己的函数来完成这个任务。
当前回答
试试这个:
ArrayLIst al = new ArrayList();
al.AddRange(array_1);
al.AddRange(array_2);
al.AddRange(array_3);
array_4 = al.ToArray();
其他回答
我想找到一种不使用数组本身以外的任何库或功能的方法。
前两个示例主要用于从头读取逻辑,但我也想知道是否会根据情况而有性能变化。
第三个例子是最实际的选择。
// 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时,它将跳过该循环中的其余代码,并继续下一轮循环,不增加整数计数器。
如果你在一个数组中有源数组,你可以使用SelectMany:
var arrays = new[]{new[]{1, 2, 3}, new[]{4, 5, 6}};
var combined = arrays.SelectMany(a => a).ToArray();
foreach (var v in combined) Console.WriteLine(v);
给了
1
2
3
4
5
6
这可能不是最快的方法,但根据用例可能适合。
我假设你正在使用自己的数组类型,而不是内置的。net数组:
public string[] merge(input1, input2)
{
string[] output = new string[input1.length + input2.length];
for(int i = 0; i < output.length; i++)
{
if (i >= input1.length)
output[i] = input2[i-input1.length];
else
output[i] = input1[i];
}
return output;
}
另一种方法是使用内置的ArrayList类。
public ArrayList merge(input1, input2)
{
Arraylist output = new ArrayList();
foreach(string val in input1)
output.add(val);
foreach(string val in input2)
output.add(val);
return output;
}
这两个例子都是c#。
如果有人正在寻找如何合并两个图像字节数组:
private void LoadImage()
{
string src = string.empty;
byte[] mergedImageData = new byte[0];
mergedImageData = MergeTwoImageByteArrays(watermarkByteArray, backgroundImageByteArray);
src = "data:image/png;base64," + Convert.ToBase64String(mergedImageData);
MyImage.ImageUrl = src;
}
private byte[] MergeTwoImageByteArrays(byte[] imageBytes, byte[] imageBaseBytes)
{
byte[] mergedImageData = new byte[0];
using (var msBase = new MemoryStream(imageBaseBytes))
{
System.Drawing.Image imgBase = System.Drawing.Image.FromStream(msBase);
Graphics gBase = Graphics.FromImage(imgBase);
using (var msInfo = new MemoryStream(imageBytes))
{
System.Drawing.Image imgInfo = System.Drawing.Image.FromStream(msInfo);
Graphics gInfo = Graphics.FromImage(imgInfo);
gBase.DrawImage(imgInfo, new Point(0, 0));
//imgBase.Save(Server.MapPath("_____testImg.png"), ImageFormat.Png);
MemoryStream mergedImageStream = new MemoryStream();
imgBase.Save(mergedImageStream, ImageFormat.Png);
mergedImageData = mergedImageStream.ToArray();
mergedImageStream.Close();
}
}
return mergedImageData;
}
连接多个数组的简单代码:
string[] arr1 = ...
string[] arr2 = ...
string[] arr3 = ...
List<string> arr = new List<string>(arr1.Length + arr2.Length + arr3.Length);
arr.AddRange(arr1);
arr.AddRange(arr2);
arr.AddRange(arr3);
string[] result = arr.ToArray();