简体   繁体   English

C#2-d数组串联

[英]C# 2-d array concatenation

Is there a more performant way to concatenate 2-d arrays than this? 有没有比这更好的连接二维数组的方法了?

  static void Main(string[] args)
    {

        int[][] array1 = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 } } ;             

        int[][] array2 = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 } };

        int[][] array3 = Concat(array1, array2);

    }

    private static int[][] Concat(int[][] array1, int[][] array2)
    {
        int array1Length = array1.Length;
        int array2Length = array2.Length;

        int[][] result = new int[array1Length + array2Length][];
        int i = 0;
        for (; i < array1Length; i++)
            result[i] = array1[i];

        for (; i < array2Length + array1Length; i++)
            result[i] = array2[i - array1Length];

        return result;    



    }

Edit: I would like to know if that's a good practice of deep concat of 2d array 编辑:我想知道这是否是2d数组的深度连接的一个好习惯

        private static int[][] DeepConcat(int[][] array1, int[][] array2)
    {
        int array1Length = array1.Length;
        int array2Length = array2.Length;

        int[][] result = new int[array1Length + array2Length][];
        int i = 0;
        for (; i < array1Length; i++)
        {
            result[i] = new int[array1[i].Length];
            for (int j = 0; j < array1[i].Length; j++)
            {
                result[i][j] = array1[i][j];
            }
        }
        for (; i < array2Length + array1Length; i++)
        {
            result[i] = new int[array2[i - array1Length].Length];
            for (int j = 0; j < array2[i - array1Length].Length; j++)
            {
                result[i][j] = array2[i - array1Length][j];
            }

        }
        return result;

    }

You could use a linked list of int[] instead so you don't need to re-allocate any new memory. 您可以改用int[]的链接列表,这样就无需重新分配任何新内存。

See LinkedList<T> , or if it doesn't perform exactly as you want for concat you can make your own easily. 请参阅LinkedList<T> ,或者如果它的性能不完全符合concat的要求,则可以轻松制作自己的。

Your issue can be simplified to the same question as this one . 您的问题可以简化为与问题相同的问题。

So my solution for you would be: 因此,我为您提供的解决方案是:

    private static int[][] Concat(int[][] array1, int[][] array2)
    {
        int[][] result = new int[array1.Length + array2.Length][];

        array1.CopyTo(result, 0);
        array2.CopyTo(result, array1.Length);

        return result;
    }

**I can't find the comment link below the original question. **我找不到原始问题下方的评论链接。 So editing my own post.* 所以编辑我自己的帖子。*

Mustafa, I would like to ask you: Do you intend to copy arrays in new memory? 穆斯塔法,我想问你:您打算在新内存中复制阵列吗? Because your original concat method only copy array references. 因为您原始的concat方法仅复制数组引用。 So after creating array3 (using original concat()), if you change anything in array1 and array2, then array3 will change as well. 因此,在创建array3(使用原始concat())之后,如果更改了array1和array2中的任何内容,那么array3也将发生变化。 I hope you are aware of that. 希望您知道这一点。 If you intended to have separate memory block for array3, then only use my method (or deltreme's method). 如果您打算为array3提供单独的内存块,则仅使用我的方法(或deltreme的方法)。

I doubt this will be alot faster, but perhaps it's a bit more readable: 我怀疑这会更快,但是也许可读性更高:

int[][] array3 = new int[array1.Length + array2.Length][];

Array.Copy(array1, 0, array3, 0, array1.Length);
Array.Copy(array2, 0, array3, array1.Length, array2.Length);

Edit: in a critical part of your application, it might be worth it - it's a shame Buffer.BlockCopy doesn't work on "nested" int[] arrays :( 编辑:在应用程序的关键部分,这可能是值得的-这是一个令人遗憾的Buffer.BlockCopy在“嵌套” int []数组上不起作用:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM