Sprache: C#
This Method can be used to merge multiple arrays from the same class to one.
Koopakiller created a more efficent alternative with LINQ, which can be found here.
http://dotnet-snippets.de/snippet/linq-erweiterung-selectmany-ohne-parameter/10000
Here is some sample code to test this:
[code]
string[] s1 = new string[] { "Dies", "ist", "ein"};
string[] s2 = new string[] { "Versuch", "Stringarrays" };
string[] s3 = new string[] { "einfach", "zu", "vereinen!" };
string[] s4 = new string[] { "Es", "können", "beliebig", "viele", "Arrays", "übergeben", "werden!" };
var result = Snippet.MergeArrays(s1, s2, s3, s4);
Snippet.ShowAll(result);
Console.WriteLine();
int[] x = new int[] { 10, 2, 4, 5 };
int[] y = new int[] { 1, 3, 6, 7 };
int[] z = new int[] { 8, 9, 12, 11, 13 };
var ints = Snippet.MergeArrays(x,y,z);
Snippet.ShowAll(ints);
Console.ReadKey();[/code]
class Snippet
{
// This Method merges Arrays from the same class.
// Array looks like this
// [0] [1] [2]
// [0] "Dies" "ist" "ein"
// [1] "Versuch" "Stringarrays"
// [2] "einfach" "zu" "vereinen"
// ...
public static T[] MergeArrays<T>(params T[][] arrays)
{
// new size for the merged array "result"
var result = new T[arrays.Sum(array => array.Length)];
// helper variable to know, where to insert the different arrays in the result array
int insertAt = 0;
// Now the handedover two dimensional array "arrays" is iterated and merged in array "result"
for (int i = 0; i < arrays.Length; i++)
{
//Copy array i to result at position xy
arrays[i].CopyTo(result, insertAt);
// new insert position for the next loop, resulting out of the lengths from just inserted array
insertAt += arrays[i].Length;
}
return result;
}
// This Method is just there to show the results on the Console Display.
public static void ShowAll<T>(T[] array)
{
Console.WriteLine(string.Join(" ", array));
}
}
class Snippet
{
// This Method merges Arrays from the same class.
// Array looks like this
// [0] [1] [2]
// [0] "Dies" "ist" "ein"
// [1] "Versuch" "Stringarrays"
// [2] "einfach" "zu" "vereinen"
// ...
public static T[] MergeArrays<T>(params T[][] arrays)
{
// new size for the merged array "result"
var result = new T[arrays.Sum(array => array.Length)];
// helper variable to know, where to insert the different arrays in the result array
int insertAt = 0;
// Now the handedover two dimensional array "arrays" is iterated and merged in array "result"
for (int i = 0; i < arrays.Length; i++)
{
//Copy array i to result at position xy
arrays[i].CopyTo(result, insertAt);
// new insert position for the next loop, resulting out of the lengths from just inserted array
insertAt += arrays[i].Length;
}
return result;
}
// This Method is just there to show the results on the Console Display.
public static void ShowAll<T>(T[] array)
{
Console.WriteLine(string.Join(" ", array));
}
}
Alte URL:
/snippet/generic-array-merge-generische-arrays-zusammenfuegen/12050
Da du ein Array eines Arrays hast kannst du auch LINQs SelectMany dafür einsetzen. Siehe auch mein Snippet dafür:
http://dotnet-snippets.de/snippet/linq-erweiterung-selectmany-ohne-parameter/10000
Den Code der ShowAll-Methode könnte man in einer Zeile zusammen fassen:
[code]Console.WriteLine(string.Join(“ „, array));[/code]
Hallo Koopakiller,
danke für dein Feedback! Ich stehe noch am Anfang der Programmierung und bin immer dankbar für Tipps.
Ich werde morgen mal versuchen deine Vorschläge umzusetzen.
@Koopakiller
Deine Variante ist echt schlank! Nochmal danke für´s Heads-up. Ich habe deinen Link in die Beschreibung eingefügt und die ShowAll() Methode angepasst.
Gern geschehen 🙂
Du scheinst übrigens den Header der ShowAll-Methode zweimal eingefügt zu haben.
Oha, nun sollte es passen! 😉