0

I am translating an old VC++ code to C#. In the C++ program, I have a three dimensional array, with the third dimension specified at run-time,

int * init_ltr [26][28];

int init_ltr_size = x;      // gets assigned to something

// array allocated and initialized
for (i=0; i < 26; i++)
    for (j=0; j < 28; j++)
        init_ltr[i][j] = new int [init_ltr_size];

I can then reference items later like:

init_ltr[firstchar-'a'][secchar-'a'][ix] = wordnum;

How do I do this in C#? I tried declaring it as int [] init_ltr [26][28] but the compiler didn't like that. If possible, I would like to retain a three-dimensional array (instead of using lists or something else) so I don't have to go and change tons of my code.

2
  • 1
    "...instead of using lists ..." - You do realise that List with its Item property allows for [ ] shorthand so no code change is required Commented May 1, 2017 at 4:15
  • Anyway, your problem isn't "dynamic" so you don't need "lists" Commented May 1, 2017 at 6:35

3 Answers 3

1

In c# you have two options, jagged arrays and multidimensional arrays. Multidimensional arrays tend to have better syntax, see below:

Jagged array:

int[][][] init_ltr = new int[26][][];

int init_ltr_size = x;

// array allocated and initialized
for (i=0; i < 26; i++)
    init_ltr[i] = new int[28][];
    for (j=0; j < 28; j++)
        init_ltr[i][j] = new int[init_ltr_size];

init_ltr[0][0][0] = 0;

Multidimensional array:

int[,,] init_ltr = new int[26, 28, x];
init_ltr[0,0,0] = 0;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the jagged array solution is closest to what I was trying to do.
1

C#

    var size1 = 26;
    var size2 = 28;

    var initLtr = new int[size1][][];
    var initLtrSize = 33;      // gets assigned to something

    for (var i = 0; i < size1; i++)
    {
        initLtr[i] = new int[size2][];
        for (var j = 0; j < size2; j++)
            initLtr[i][j] = new int[initLtrSize]; //it could even be different sizes in each array e.g: new int[i + j]
    }

    initLtr[0][0][0] = 3;

It's just a matter or initialising each of the arrays inside the jagged array.

Comments

0

An alternative to the solution posted by DesertFox. Since the first 2 dimensions are hardcoded, you can use 2D array and use jagged array for the 3rd dimension. I use [3, 5] for example. You can change it to [26, 28].

int[,][] init_ltr = new int[3, 5][]; // init_ltr [26, 28][]
int init_ltr_size = 2; // Assign some value
for (var i = 0; i <= init_ltr.GetUpperBound(0); i++)
    for (var j = 0; j <= init_ltr.GetUpperBound(1); j++)
    {
        init_ltr[i, j] = new int[init_ltr_size];
        for (var k = 0; k < init_ltr[i, j].Length; k++)
            init_ltr[i, j][k] = (i + 1) * (j + 1) * (k + 1); // Assign some value
    }

// Print values of arrays. Use int_ltr[i, j][k]
for (var i = 0; i <= init_ltr.GetUpperBound(0); i++)
    for (var j = 0; j <= init_ltr.GetUpperBound(1); j++)
        for (var k = 0; k < init_ltr[i, j].Length ; k++)
            Console.WriteLine(init_ltr[i, j][k]);

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.