Olá, preciso fazer um método recursivo para verificar se uma matriz de inteiros é uma matriz identidade.
Ex da matriz identidade
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Fiz meu método assim
public static boolean isIdentidade(int[][] m) {
if (m == null) {
throw new NullPointerException();
}
if (m.length != m[0].length) {
throw new IllegalArgumentException();
}
return isIdentidade(m, 0, 0);
}
private static boolean isIdentidade(int[][] m, int i, int j) {
if (i >= m.length) {
return true;
}
if (m[i][j] != 1) {
return false;
}
return isIdentidade(m, i+1, j + 1);
}
Ele funciona parcialmente, ele verifica as posições dos 1, porém não consigo fazer ele verificar se o restante é zero. No exemplo da matriz acima ele retorna true ou falso caso algum elemento 1 estiver fora da posição certa, porém se a matriz for assim ele vai retornar true igual:
1 0 1 0
0 1 0 0
1 0 1 0
0 1 0 1