Creating an Array from a multidimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • @ nospam.com

    #1

    Creating an Array from a multidimensional array

    Hi,

    If Im using Jagged arrays, the following is possible:

    int[][] arr = new int[10]

    for(int i=0; i<10; i++) {
    arr[i] = new int[10];
    }

    int[] arrB = arr[3];

    How can I achieve the same thing (i.e. create a single dimensional array
    by assigning a copy of some row of a multidimensiona l array to it) using
    multidimensiona l arrays? Also, in the last line above, a row is not
    being assigned or copied by value right? A copy of the reference to the
    4th row is being assigned to arrB? Thanks!

    Oh and I dont want to use a For loop.. I mean, I don't want to do the
    following:

    int[] arrB = new int[arr.GetLength(0 )];

    for(int i=0; i<10; i++) {
    arrB[i] = arr[4, i];
    }

    Thanks

    -Phil

  • Andre

    #2
    Re: Creating an Array from a multidimensiona l array

    Use System.Array.Co py

    For example:

    Array.Copy(arr, arr.GetLength(1 )*4, arrB, 0, arr.GetLength(1 ));


    -Andre

    @ wrote:
    [color=blue]
    > Hi,
    >
    > If Im using Jagged arrays, the following is possible:
    >
    > int[][] arr = new int[10]
    >
    > for(int i=0; i<10; i++) {
    > arr[i] = new int[10];
    > }
    >
    > int[] arrB = arr[3];
    >
    > How can I achieve the same thing (i.e. create a single dimensional array
    > by assigning a copy of some row of a multidimensiona l array to it) using
    > multidimensiona l arrays? Also, in the last line above, a row is not
    > being assigned or copied by value right? A copy of the reference to the
    > 4th row is being assigned to arrB? Thanks!
    >
    > Oh and I dont want to use a For loop.. I mean, I don't want to do the
    > following:
    >
    > int[] arrB = new int[arr.GetLength(0 )];
    >
    > for(int i=0; i<10; i++) {
    > arrB[i] = arr[4, i];
    > }
    >
    > Thanks
    >
    > -Phil
    >[/color]

    Comment

    Working...