问题
Saw similiar (not the same, though) quesion yesterday and I realized I do not know it either.
Lets say there we have a 2 dimensional "int[,]" array with elements like this:
1,2,3,4,5
5,4,3,2,1
And I want to reverse elements order in both dimensions rows:
5,4,3,2,1
1,2,3,4,5
Or just in one:
5,4,3,2,1
5,4,3,2,1
Only way I could think of is doing that manually in for statement. Any other idea?
回答1:
You can try to use Array.Reverse for each line of array.
Update:
If you can use array of arrays instead of two dimensional array:
var array = new int[2][];
array[0] = new int[]{1, 2, 3, 4, 5};
array[1] = new int[]{5, 4, 3, 2, 1};
for (int i = 0; i < 2; i++)
Array.Reverse(array[i]);
回答2:
List have this operation built in.
List objects = new List(); objects.AddRange(arrayobjects); objects.Reverse();
来源:https://stackoverflow.com/questions/4982860/reverse-order-of-elements-in-2-dimensional-array