Swap Rows and Columns of 2D Array in C#
Author: Markus Weller IN: Array, C#, Column, Row Comments: 0
Background
The following methods seem to be very simple, but there are many unanswered questions in Q&A websites like “Stackoverflow” of people who are searching for a simple and quick way to swap entire columns or rows of 2D arrays.That’s why I decided to publish my methods to do this.
How the Methods work
- Clone the Array
- Check for every X value of the array if it matches with the A.
- If it matches replace it with the X value of B of the original array.
- Check for every X value of the array if it matches with the B.
- If it matches replace it with the X value of A of the original array.
How the Methods are used
“throw new IndexOutOfRangeException();”
The methods will return an array where the rows / columns are swapped.

Swap 2 Rows
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
/// <summary> /// Swaps two rows of a two dimensional array of strings. /// </summary> /// <param name="array">The Array which should be changed</param> /// <param name="a">this row will get swaped with the row b</param> /// <param name="b">this row will get swaped with the row a</param> /// <returns>The input array, but a and b are swaped</returns> static string[,] swapRows(string[,] array, int a, int b) { //clone the array string[,] clone = (string[,])array.Clone(); //fill A of the clone with B of the original for (int i = 0; i <= clone.GetLength(0) - 1; i++) { if (i == a) { for (int j = 0; j <= clone.GetLength(1) - 1; j++) { if (b <= clone.GetLength(0) - 1 && b >= 0) { clone[i, j] = array[b, j]; } else { throw new IndexOutOfRangeException(); } } } } //fill B of the clone with A of the original for (int i = 0; i <= clone.GetLength(0) - 1; i++) { if (i == b) { for (int j = 0; j <= clone.GetLength(1) - 1; j++) { if (a <= clone.GetLength(0) - 1 && a >= 0) { clone[i, j] = array[a, j]; } else { throw new IndexOutOfRangeException(); } } } } return clone; } |
Swap 2 Columns
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
/// <summary> /// Swaps two columns of a two dimensional array of strings. /// </summary> /// <param name="array">The Array which should be changed</param> /// <param name="a">this column will get swaped with the row b</param> /// <param name="b">this column will get swaped with the row a</param> /// <returns>The input array, but a and b are swaped</returns> static string[,] swapColumns(string[,] array, int a, int b) { //clone the array string[,] clone = (string[,])array.Clone(); //fill A of the clone with B of the original for (int i = 0; i <= clone.GetLength(0) - 1; i++) { for (int j = 0; j <= clone.GetLength(1) - 1; j++) { if (j == a) { if (b <= clone.GetLength(1) - 1 && b >= 0) { clone[i, j] = array[i, b]; } else { throw new IndexOutOfRangeException(); } } } } //fill B of the clone with A of the original for (int i = 0; i <= clone.GetLength(0) - 1; i++) { for (int j = 0; j <= clone.GetLength(1) - 1; j++) { if (j == b) { if (a <= clone.GetLength(1) - 1 && a >= 0) { clone[i, j] = array[i, a]; } else { throw new IndexOutOfRangeException(); } } } } return clone; } |




