Cocodrilo
Cocodrilo

Reputation: 191

Reverse order of elements in 2-dimensional array

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?

Upvotes: 0

Views: 4459

Answers (2)

Marat Faskhiev
Marat Faskhiev

Reputation: 1302

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]);

Upvotes: 1

tourdownunder
tourdownunder

Reputation: 1829

List have this operation built in.

List objects = new List(); objects.AddRange(arrayobjects); objects.Reverse();

Upvotes: 0

Related Questions