[2 [2
c#'s concat
x = new int [] {1,2,3}; int [] y = new int [] {4,5}; int [] z = new int [x.length y.length]; array.copy(x,z,x.length); array.copy(y,0,z,x.length,y.length); debug.assert(z.SequenceEqual(new Int [] {1,2,2,3,4,5}));
此方法直接分配了一個足夠大小的新數組,以保持[ y
。 It then leverages
int[] x = new int[] { 1, 2, 3 };
int[] y = new int[] { 4, 5 };
int[] z = new int[x.Length y.Length];
Array.Copy(x, z, x.Length);
Array.Copy(y, 0, z, x.Length, y.Length);
Debug.Assert(z.SequenceEqual(new int[] { 1, 2, 3, 4, 5 }));
y into z
.這避免了創建中間數組的創建,從而可以更快地執行,尤其是使用較大的數據集。
重要考慮:
concat 仍然適用於較小的數組,而性能並不關鍵,上面的方法為涉及大型數組的方案提供了實質性的性能優勢。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3