I have a multi-dimensional array and I have a typical swap function but would the function apply to any number of dimensions? For example,
public void swap(int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
This works for a regular array. But I need to swap two indices of a 2D array. Could I use the same function but just call the parameters differently?
Sample Input:
int[][] arr = {{1}, {2}, {3}};
System.out.println(arr[0][0]);
// I am confused on
// what these parameters should be
swap(arr, arr[0][0], arr[1][0]);
System.out.println(arr[0][0]);
Sample Output:
1
2