# Developers Planet

Austin DiMarzio February 2016

### Switch Maximum and Minimum values in Two Dimensional Array

I'm trying to switch the minimum and maximum values of the array.

My code:

``````public static void swap(int[][] a, int i0, int j0, int i1, int j1) {
int temp = a[i0][j0];
a[i0][j0] = a[i1][j1];
a[i1][j1] = temp;
}
public static void main(String args[]){
int max = 0, min = 0, tmpI = 0, tmpJ = 0, tmpI1 = 0, tmpJ1 = 0;
int[][] a = {{10,9,20} , {2,10,10}};
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (min > a[i][j]) {
min = a[i][j];
tmpI1 = i; tmpJ1 = j;
}
if (max < a[i][j]) {
max = a[i][j];
tmpI = i; tmpJ = j;
}
}
}
System.out.println(Arrays.deepToString(a));
swap(a,tmpI1,tmpJ1,tmpI,tmpJ);
System.out.println(Arrays.deepToString(a));
}
``````

Output:

``````[[10, 9, 20], [2, 10, 10]]
[[20, 9, 10], [2, 10, 10]]
``````

It is switching only to the first number for some reason. Any help? I'm new to java.

Elliott Frisch February 2016

The issue is with your initial value of `min` (`0`). When you say,

``````if (min > a[i][j]) {
``````

it will never be true (because `0` is less than your numbers). Change the declaration of `min` to something like

``````min = Integer.MAX_VALUE
``````

because every `int` value will be less than that.