C# rows of multi-dimensional arrays
Solution 1
You can't pass a row of a rectangular array, you have to use a jagged array (an array of arrays):
int[][] foo = new int[6][];
for(int i = 0; i < 6; i++)
foo[i] = new int[4];
int[] least = new int[6];
for(int i = 0; i < 6; i++)
least[i] = FindLeast(foo[i]);
EDIT
If you find it so annoying to use a jagged array and desperately need a rectangular one, a simple trick will save you:
int FindLeast(int[,] rectangularArray, int row)
Solution 2
You don't, with a rectangular array like that. It's a single object.
Instead, you'd need to use a jagged array, like this:
// Note: new int[6][4] will not compile
int[][] foo = new int[6][];
for (int i = 0; i < foo.Length; i++) {
foo[i] = new int[4];
}
Then you can pass each "sub"-array:
int[] least = new int[foo.Length];
for(int i = 0; i < 6; i++)
{
least[i] = FindLeast(foo[i]);
}
Note that there's no need to pass foo[i]
by reference1, and also it's a good idea to assign local variables values at the point of declaration, when you can. (It makes your code more compact and simpler to understand.)
1 If you're not sure about this, you might want to read my article on parameter passing in C#.
CodeKingPlusPlus
Updated on July 21, 2022Comments
-
CodeKingPlusPlus almost 2 years
In the C# programming language, how do I pass a row of a multi-dimensional array? For example, suppose I have the following:
int[,] foo; foo = new int[6,4]; int[] least; least = new int[6]; for(int i = 0; i < 6; i++) { least[i] = FindLeast(ref foo[i]); //How do I pass the ith row of foo??? }
Also, could anyone explain to me the benefit of having rectangular and jagged arrays in C#? Does this occur in other popular programming languages? (Java?) Thanks for all the help!
-
BlackBear about 12 yearsYou've beaten me by seconds! :P
-
CodeKingPlusPlus about 12 yearsCan I pass a rectangular array as a jagged array?
-
CodeKingPlusPlus about 12 yearsCan I pass a rectangular array to a function that accepts jagged arrays?
-
BlackBear about 12 years@CodeKingPlusPlus: No, you can't
-
Niels Abildgaard almost 10 yearsSo basically this isn't a solution :)
-
Niels Abildgaard almost 10 yearsThis, to me, seems like a non-solution. What can be done in the case of a rectangular array?
-
Jon Skeet almost 10 years@NielsAbildgaard: Nothing, basically. The answer to the question is "you can't do that". It may not be an answer the OP wants to hear, but it's the correct answer.
-
Niels Abildgaard almost 10 years@JonSkeet That's just not true. It might require more code and be cumbersome (which should be taken into account), but it is definitely possible.
-
Jon Skeet almost 10 years@NielsAbildgaard: You can't pass it as an array, which is what the OP appeared to be looking for. You could create your own wrapper type, but there's no indication that that would help the OP. However, if you think you have a better answer, why not add it yourself?
-
Niels Abildgaard almost 10 years@JonSkeet Just did ;)
-
Jon Skeet almost 10 yearsThat's copying the array contents though, which doesn't do the same thing. In particular, if
FindLeast
modifies the array, then that modification won't be visible in the result. Additionally, the result offoo.Row(i)
isn't classified as a variable, so the code won't compile. You simply can't treat a rectangular array as if it were a jagged array - arrays just don't work like that. -
Niels Abildgaard almost 10 yearsI do not understand your comment about compilation. The code runs and compiles.
-
Niels Abildgaard almost 10 yearsYou are absolutely right! This not passing a reference to a row (which cannot be done). Assuming that
FindLeast
does not modify the row, this should work. I will rename the method, though :) -
Jon Skeet almost 10 yearsIt may solve the OP's
FindLeast
example, but it doesn't address the more general question of passing a single row of a rectangular array as a single-dimensional array, which is (as I keep saying) impossible. -
Niels Abildgaard almost 10 yearsIt does pass a row. It doesn't pass a reference to the same row. It passes a copy of the row. I think we are in agreement about what C# can and cannot do. I disagree with whether or not reference passing is required for an answer to the original question :)
-
Jon Skeet almost 10 yearsI think that is implicit, to be honest. "Pass a row of a multi-dimensional array" and "pass a copy of a row of a multi-dimensional array" are two different things. I think it's a lot clearer to say "You can't pass a row" than to claim that you can, and actually just pass a copy. I still maintain that the best answer to the OP's question is "you can't".
-
kdbanman over 8 yearsThe "rewritten" code doesn't use your
CopyRow
method. It actually doesn't appear to be rewritten in any way. Also, the order of strings passed to theArgumentOutOfRangeException
constructor is wrong - the parameter name should come first. -
Niels Abildgaard about 8 yearsThat was a simple typo -- (
Row
->CopyRow
). Fixed.