0

I'm relatively new to C# and the way it handles multidimensional arrays compared to Java is screwing with me.

I'm sure there's a simple solution and that I'm gonna feel really stupid for not realizing it, but I can't seem to find an answer online or figure it out myself.

Consider the following code snippet in java:

Object firstElement(Object[] arr) {
   return arr[0];
}

This would return the first element of an array of any number of dimensions; however, in C# this will throw out an error for greater than one dimension because it doesn't recognize a multidimensional array as an object array. The only way to do this I found was by casting the multidimensional array to a System.Array and then using the following code:

object firstElement(Array arr) {
   foreach (object obj in arr)
      return obj;
}

Is it even possible to do this without a foreach loop in the function? I have tried returning the object using arr.GetValue(0) but this will throw an error again if the array is not one dimensional. Thanks for helping this C# newbie out!

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
  • 4
    Java doesn't *have* multidimensional arrays. Since it has no such feature, you can really compare a C# multidimensional array to it. – Servy Jul 30 '19 at 13:24
  • 6
    @AndrewTobilko No. In Java (and C#) you can write an array of arrays, which is very different from a multidimensional array in not only syntax but behavior. – Servy Jul 30 '19 at 13:27
  • No it is not Edward, see this [link](https://stackoverflow.com/questions/597720/what-are-the-differences-between-a-multidimensional-array-and-an-array-of-arrays) – Nexevis Jul 30 '19 at 13:28
  • The first element (i.e. `[0,0`) is available using `arr.Cast().FirstOrDefault()`, if that is what you are trying to do. _Where `object` can be changed for whatever your array type is._ – mjwills Jul 30 '19 at 13:30
  • As @Servy said there are no real multidimensional arrays in java. They are more like [arrays of arrays](https://www.geeksforgeeks.org/multidimensional-arrays-in-java/). In C# there are multidimensional arrays, but the are their [own separate type](https://stackoverflow.com/a/275097/11679735) and thus have a few restrictions. The more important question here is do you really want/need a multidimensional array or do you want a list of arrays? – FeRaaC Jul 30 '19 at 13:34
  • 1
    @Servy and Nexevis: Sorry, my mistake. I was told that they were the same thing but clearly that is not the case. I declared the array with the jagged array syntax and my code ran fine. Thank you! – Edward Pinkston Jul 30 '19 at 13:36
  • 1
    @Servy in Java, "an array of arrays" and "a multidimensional array" are synonyms because of the absence of other concepts – Andrew Tobilko Jul 30 '19 at 13:37

1 Answers1

2

C# expects you to address every dimension in a multidimensional array even if you are meaning to access, say, [0,0].

Object firstElement(Object[] arr) {
   return arr[0];
}

For this reason, the code above will throw at compile time.

this code only takes one-dimensional arrays. Try this instead:

Object firstElement(Object[][] arr) {
   return arr[0][0];
}

Adjust amount of brackets according to the amount of dimensions.

It is also more common to use the keyword-aliases for primitives like object or int. I do suspect you will replace the Object with an actual class / struct instance or primitive at some point.

object firstElement(object[][] arr) {
   return arr[0][0];
}

What I would suggest you reading up on are the two different types of "multidimensional"-arrays: Jagged and actual multidimensional-arrays. The key difference is that each row has to have the same amount of columns in a multidimensional-array whereas the jagged array can be irregular in this regard.

/edit: I seem have to misunderstood your intention. I believe you try to get every first element of each row. If that is the case then try this one:

List<object> firstElements(object[][] arr) 
{
    List<object> firsts = new List<object>();

    for(int i = 0; i < arr.length; i++)
    {
       firsts.Add(arr[i][0]);
    }

    return firsts;
}
Skelp
  • 121
  • 5