5

I would like to copy a specific row from a multidimensional array to a new one dimensional array that can be used somewhere else in my code.

Input:

Multidimensional array[3,3]:

33 300 500,
56 354 516,
65 654 489,

Required output:

Single Dimension Array (second line)

56 354 516
2
  • Thank you everyone for the replies. have to apologise, I only realized now, I over siplified the question, I should have been more exact. My multidimensional array contains "double" values. Sorry, still new to c#. You may edit the question to suit the question better. Commented Jan 25, 2016 at 6:54
  • I don't think array containing double, string or int will matter. Commented Jan 25, 2016 at 6:57

4 Answers 4

5

This is a case where the Buffer.BlockCopy may come handy:

int[,] original = new int[3, 3]
{
    { 33, 300, 500 },
    { 56, 354, 516 },
    { 65, 654, 489 }
};

int[] target = new int[3];
int rowIndex = 1; //get the row you want to extract your data from (start from 0)
int columnNo = original.GetLength(1); //get the number of column
Buffer.BlockCopy(original, rowIndex * columnNo * sizeof(int), target, 0, columnNo * sizeof(int));

You will get in your target:

56, 354, 516
Sign up to request clarification or add additional context in comments.

Comments

2
var source = new int[3, 3]
{
    { 33, 300, 500 },
    { 56, 354, 516 },
    { 65, 654, 489 }
};
// initialize destination array with expected length
var dest = new int[source.GetLength(1)];

// define row number
var rowNumber = 1;

// copy elemements to destination array
for (int i = 0; i < source.GetLength(1); i++)
{
    dest[i] = (int) source.GetValue(rowNumber, i);
}

3 Comments

Hi Vadim, thanks for the help, do you mind if I edit the question? I like your solution, however I cant get it to work for "double" values.
@Crysthius What's the problem? Just change all int lines to the double: int[3, 3] -> double[3, 3], new int[source.GetLength(1)]; -> new double[source.GetLength(1)];, (int) source.GetValue(rowNumber, i); -> (double) source.GetValue(rowNumber, i);
Hi Vadim, thanks for the help, it worked. Kind Regards
1

Should be something like this:

        int[][] arrayComplex = {new[] {33, 300, 500},new []{56, 354, 516}, new []{65, 654, 489}};
        int[] arraySingle = new int[3];
        for (int i = 0; i < arrayComplex[1].Length; i++)
        {
            arraySingle[i] = arrayComplex[1][i];
        }

        foreach (var i in arraySingle)
        {
            Console.Write(i + "  ");
        }

Comments

0

In .NET Core 2.1 and later, you can use MemoryMarshal.CreateSpan<T>(T, Int32) method to create a linear Span from a multidimensional array, then use Span<T>.CopyTo(Span<T>) method to copy the required data into or from the multidimensional array.

int[,] table =
{
    { 33, 300, 500 },
    { 56, 354, 516 },
    { 65, 654, 489 }
};

int[] ints1 = new int[3];
int[] ints2 = [1, 2, 3, 4];

var tableSpan = MemoryMarshal.CreateSpan(ref table[0, 0], table.Length);
// tableSpan is linear here: [33, 300, 500, 56, 354, 516, 65, 654, 489]

tableSpan.Slice(3, ints1.Length).CopyTo(ints1);
// ints1 is now: [56, 354, 516]

ints2.AsSpan().CopyTo(tableSpan.Slice(3, ints2.Length));
// table is now:
// {
//     { 33, 300, 500 },
//     { 1, 2, 3 },
//     { 4, 654, 489 }
// }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.