2

I would like to create a multidimensional array in c#, but I`m not sure how. The array should look like this:

array1, array2, array3
array4, array5, array6
array7, array8, array9

The each small array will store 3 ints. I managed to create a multidimensional array that stores 1 array on each line, but I need to store 3 arrays on each line.

The code is below:

int[][] jaggedArray = new int[3][];

jaggedArray[0] = new int[] {  1,  3,  5 };
jaggedArray[1] = new int[] {  0,  2,  4 };
jaggedArray[2] = new int[] { 11, 22, 33 };
7
  • Wait, do you want a 2d array of arrays, or do you want a jagged array of arrays, or do you want a 3d array? Commented Mar 19, 2020 at 8:24
  • Pleas dont copy code from other sites to explain what you want to to and what you already achieve. Commented Mar 19, 2020 at 8:25
  • not really sure, I need an array that stores 3 lines of arrays,and on each line I need to store 3 arrays Commented Mar 19, 2020 at 8:27
  • Your description is confusing, You want an array stores 3 int, but your posted code contains 5,4,2 int. Please explain desired data structure step by step clearly. Commented Mar 19, 2020 at 8:27
  • Do any of your dimensions have varying sizes? Like is [0,0] a 5-entry array, and [0,1] a 10-entry array? Commented Mar 19, 2020 at 8:28

3 Answers 3

4

Your description corresponds to 2d array of arrays int[,][]:

 int[] array1 = new int[] {1, 2, 3};
 ... 
 int[] array9 = new int[] {89, 562, 356};

 ...  

 // 2d array of arrays (array1..array9)
 int[,][] array = new int[,][] {
   { array1, array2, array3, },
   { array4, array5, array6, },
   { array7, array8, array9, },  
 };
Sign up to request clarification or add additional context in comments.

Comments

0

You can declare as given below. you can use jagged arrays.

int[][,] jaggedArray4 = new int[3][,] 
{
    new int[,] { {1,3,3}, {5,7,7},{1,2,3} },
     new int[,] { {1,3,3}, {5,7,7},{1,2,3} },
     new int[,] { {1,3,3}, {5,7,7},{1,2,3} }
};

Console.WriteLine(jaggedArray4[0][1,0]);//displayed first row , 2nd element array's first element

Comments

0

If you always have 3x3x3, you can use a 3d array:

int[,,] my3dArray = new int[3,3,3];

And you can access it like so:

my3dArray[0,0,0] = 5;
my3dArray[0,0,1] = 2;
my3dArray[1,2,0] = 41;
Console.WriteLine(myArray[1,2,0]); // prints 41

If each dimension is a variable size, you could used an array of arrays of arrays (kind of a 3d jagged array):

int[][][] myJagged = new int[3][][];
myJagged[0] = new int[3][];
myJagged[0][0] = new int[] {1, 2, 3}; // etc. until you've initialized all the arrays at all levels

Console.WriteLine(myJagged[0][0][2]); // prints 3

Alternatively, if only your last dimension is a variable size (or if it's easier to work with), you could use a 2d array of arrays:

int[,][] myArray = new int[3,3][];
myArray[0,0] = new int[] { 5, 2, 41 };
Console.WriteLine(myArray[0,0][2]); // prints 41

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.