Reverse order of elements in 2-dimensional array

限于喜欢 提交于 2020-01-07 05:08:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!