2

Trying to write a string to multi dimensional int map.

1,1,1,1,1,1,1,1,1,1,1
1,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1

Yeah you can pretty much guess what that's for. A collision map. But anyways, this info is store din a file. Now I got the file in, converted and blah blah. I'll just show the code and show where im having the problem, probably easier that way:

// Convert a string to multi-dim int array,
// format: 1,1,1,1|1,0,0,1|1,1,1,1|
private int[,] ConvertToMapArray(String data) 
{
    // Split to iterate rows and get row count.
    string[] rows = data.Split('|');

    // Initialize return data.
    int[,] ret;

    // Set row count to 0, increment each loop.
    int rc = 0;

    foreach (string row in rows)
    {
        // Split each number and iterate. Convert to int32 and and store in its own int array.
        string[] items = row.Split(',');

        // Initialize int array with proper row count.
        int[] newRow = new int[ items.Count() ];

        // Count set to 0 for foreach iteration, must increment.
        int ic = 0;

        foreach (string item in items )
        {
            // Convert string to int.
            int i int.Parse(item);

            // Add to newRow int array.
            newRow[ic++] = i;
        }

        // Add new int row to return multi-dim array.
        ret[rc++] = newRow;   /// <--- this doesnt work
    }

    return ret;
}
AgentFire
  • 8,944
  • 8
  • 43
  • 90
RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
  • 1
    Try setting `ret` to an `int[][]` which is an array of arrays and it should then work. – matthewr Nov 13 '12 at 05:38
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 13 '12 at 05:47
  • My apologies, that was actually not the intention, but I appreciate letting me know for future knowledge – RedactedProfile Nov 13 '12 at 19:04
  • Interesting how community editing my code contains a new syntax error now -.-; `int i int.Parse(item);` uh huh lol – RedactedProfile Nov 14 '12 at 02:44

3 Answers3

2

You have Rectangular (Two dimenstional) array not Jagged array. You can learn about these array types here.

int[,] ret; //Rectangular array.

you need to assign values like this

ret[0,0] = 1;

So you have to use for loop instead of foreach to get the index or row and columns.

Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
2

You can't assign a whole row to a multi-dimensional array in C#. It's not an array of arrays. You should either:

  1. Assign to both row and column of ret directly. For example replace newRow[ic] = i with ret[rc, ic] = i

  2. Use a Jagged Array instead of multi-dimensional array. Jagged arrays are array of arrays. So you can assign a whole row to it. as in

:

int[][] ret = new int[numberOfRows][];
...
ret[rc] = newRow;
Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
1

You've declared ret as a multi-dimensional array, but you're using it as a jagged array. Change the declaration to:

int[][] ret = new int[rows.Length][];

And, just for fun, the whole thing can be reduced to a single Linq query:

private int[][] ConvertToMapArray(String data) 
{
    return data.Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries)
               .Select(row => row.Split(',')
                                 .Select(item => {int i; Int32.TryParse(item, out i); return i;})
                                 .ToArray())
               .ToArray();
}

or, a little more readably:

private int[][] ConvertToMapArray(String arrayData) 
{
    var rowDelimiter = new [] {'|'};
    return arrayData.Split(rowDelimiter, StringSplitOptions.RemoveEmptyEntries)
                    .Select(row => ConvertToMapRow(row))
                    .ToArray();
}

private int[] ConvertToMapRow(String rowData) 
{
    return row.Split(',')
              .Select(item => 
                  {
                      int i; 
                      Int32.TryParse(item, out i); 
                      return i;
                  })
              .ToArray();
}
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116