0

I have a multidimensional array which looks like this:

public static int[,] GameObjects = new int[,]
{
    //Type,X,Y,Width,Height
    {0,50,50,10,100},
    {1,50,150,10,20}
};

I'm trying to access one "row" of values and store them into a variable within a for loop:

 for (int i = 0; i < gameObjectData.Length; i++)
 {
     int[] g = gameObjectData[i];
 }

I want g to store the first array's values, so in the first loop g should store 0,50,50,10,100. The code gives the error Wrong number of indices inside []; expected 2

3
  • int[,] and int[][] are not the same. I forget the specifics (looking up now, I find it , I'll post an answer) Commented Apr 30, 2015 at 13:10
  • int[,] is not the same as int[][]. You need to copy the values of one row to a new array. Commented Apr 30, 2015 at 13:10
  • Look at this stackoverflow.com/questions/5132397/… Commented Apr 30, 2015 at 13:20

4 Answers 4

3

There is no exposed mechanism of a two dimensional array to get a single dimensional array out of it.

If you have a jagged array then it's possible:

int[][] array;
//populate array
int[] row = array[1];

If you need to have a multi-dimensional array then the best you could do is create a class to hold onto a two dimensional array and a row number and expose an indexer to access items in that row; it could appear similar to an array, but it wouldn't actually be one.

Something like this would give you the bare bones; if you wanted to make the type extend IList<T> you could do that as well.

public class ArrayRow<T> : IEnumerable<T>
{
    private T[,] array;
    private int index;
    public ArrayRow(T[,] array, int index)
    {
        this.array = array;
        this.index = index;
    }

    public T this[int i]
    {
        get { return array[index, i]; }
        set { array[index, i] = value; }
    }

    public int Count { get { return array.GetLength(1); } }

    public IEnumerator<T> GetEnumerator()
    {
        for (int i = 0; i < Count; i++)
            yield return this[i];
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}
public static ArrayRow<T> GetRow<T>(this T[,] array, int index)
{
    return new ArrayRow<T>(array, index);
}

Alternatively you could copy the value of each row in the multi-dimensional array to a new single dimensional array, but then neither will reflect changes made to the other.

Sign up to request clarification or add additional context in comments.

Comments

3

You get this error because you try to use a multidimensional array as if it was a jagged array.

Change

int[,] 

to

int[][] 

and you will be fine.
Read here about the differences between these types of arrays.

Comments

0

For multidimensional arrays you need to access them like this:

// access 0,1
gameObjectData[0,1];

// access 5,4
gameObjectData[5,4];

// So in general is
gameObjectData[x,y];

You can't access it by just giving [x], so for your question it would be:

// asuming you want row 0
int row = 0;

// this give to g the size of the row of gameObjectData
int[] g = new int[gameObjectData.getLenght(row)];

for (int i = 0; i < gameObjectData.getLenght(row); i++)
{
   g[i] = gameObjectData[row,i];
}

Comments

0

If you like one-liners, here is a LINQ one:

int rowIndex = 0;
firstRow = Enumerable.Range(0, gameObjectData.GetLength(1-rowIndex))
                     .Select(v => gameObjectData[rowIndex,v])
                     .ToArray();

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.