0

I have jagged 2D array like so:

static void Main(string[] args)
    {
        int[][,] newArray = new int[2][,];

        int[][,] waypoints = new int[4][,]   
        {
            new int[,] {{6,3,4,5,6}},
            new int[,] {{1,3,4,5,6}},
            new int[,] {{1,4,3,2,1}},
            new int[,] {{6,3,4,5,6}}
        };

        int l = 0;
        int m = 0;

        for (int i = 0; i < waypoints.Length; i++)
        {
            for (int j = 0; j < waypoints[i].GetLength(0); j++)
            {
                for (int k = 0; k < waypoints[i].GetLength(1); k++)
                {
                    if (k == 1 || k == 3)
                    {
                        // waypoints[i][j,k].CopyTo(newArray[i][j,k]);
                    }
                    l++;
                    m++;
                }   
            }
        }
        Console.ReadKey();
    }

And I need to extract from each jagged array only [0,1] and [0,3] 2D array and store that in new jagged array - newArray. Please, could you help me, how to do that. Many thanks in advance.

Desired output should look like:

int[][,] newArray = new int[2][,];
{
     new int[,] {{3,5}},
     new int[,] {{3,5}},
     new int[,] {{4,2}},
     new int[,] {{3,5}}
 };
7
  • Try something like this Foreach (int i in newArray [0,1]{//do stuff} Commented Jul 25, 2016 at 9:33
  • It's not clear how you want to store the data in newArray. Can you describe what the resultant array should be? Commented Jul 25, 2016 at 9:38
  • I've added desired output in my task. Commented Jul 25, 2016 at 10:27
  • Your arrays are somewhat unusual but it looks to me as if you are handling 3D arrays (arrays of 2D arrays), even though you don't need them because your 2D array elements all only have one element each (one row), i.e. they are 1-dimensional from a logical POV; as indicated by the double curly braces. Commented Jul 25, 2016 at 10:28
  • I know what you mean Peter but I need to keep this solution for another purposes. Commented Jul 25, 2016 at 10:30

1 Answer 1

1

Try this. Note that I expanded the size of newArray to accomodate 4 2D-arrays.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication26
{
    class Program
    {
        static void Print3DArr(int[][,] arr)
        {
            foreach(var TwoDArr in arr)
            {
                for (int lineInd = 0; lineInd < TwoDArr.GetLength(0); lineInd++)
                {
                    for (int elemInd = 0; elemInd < TwoDArr.GetLength(1); elemInd++)
                    {
                        Console.Write(TwoDArr[lineInd, elemInd] + " ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine();
                Console.WriteLine();
            }
        }

        static void Main(string[] args)
        {
            int[][,] newArray = new int[4][,];

            int[][,] waypoints = new int[4][,]
            {
                new int[,] {{6,3,4,5,6}},
                new int[,] {{1,3,4,5,6}},
                new int[,] {{1,4,3,2,1}},
                new int[,] {{6,3,4,5,6}}
            };

            Print3DArr(waypoints);

            for (int TwoDArrIndex = 0; TwoDArrIndex < waypoints.Length; TwoDArrIndex++)
            {
                newArray[TwoDArrIndex] = new int[waypoints[TwoDArrIndex].GetLength(0), 2];

                for (int LineIn2DArr = 0; LineIn2DArr < waypoints[TwoDArrIndex].GetLength(0); LineIn2DArr++)
                {
                    newArray[TwoDArrIndex][LineIn2DArr, 0] = waypoints[TwoDArrIndex][LineIn2DArr, 1];
                    newArray[TwoDArrIndex][LineIn2DArr, 1] = waypoints[TwoDArrIndex][LineIn2DArr, 3];
                }
            }

            Print3DArr(newArray);
            Console.ReadKey();
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for. Thank you very much for your time Peter!!

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.