0

Dos matrices 2x3 entera y determinar si sus contenidos son exactamente iguales. He usado el if sin equals ejemplo (matriz[i][j] == matriz2[i][j]) y esta última con el Arrays.equals y ni así logro que dé, solo me dice que las matrices no son iguales y no se cual es la razón.

public class Ejercicio19 {
public static void main(String[] args) {
    Scanner entrada = new Scanner (System.in);
    int matriz [][] = new int [2][3];
    int matriz2 [][] = new int [2][3];
    System.out.println("Ingrese valores");
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 3; j++){
            System.out.println("Ingrese numero primer matriz");
            matriz[i][j] = entrada.nextInt();
        }
    }
    for (int i = 0; i < 2 ; i++){
        for (int j = 0; j < 3; j++){
            System.out.println("Ingrese numero segunda matriz");        
            matriz2[i][j] = entrada.nextInt();
        }
    }
    for (int i = 0; i < 2; i++){
        System.out.println(" ");
        for (int j = 0; j < 3; j++){
            System.out.print(matriz[i][j]+" ");
                }
            }
    for (int i = 0; i < 2 ; i++){
        System.out.println(" ");
        for (int j = 0; j < 3; j++){
            System.out.print(matriz2[i][j]+" ");
        }
    }   
    for (int i = 0; i < 2; i++){
        for (int j = 0; j < 3 ; j++){
            if (Arrays.equals(matriz, matriz2)){
                    System.out.println("son iguales");
                    }
            else{
                System.out.println("no son iguales ");
            }
        }   
    }
}

}

4
  • 1
    matriz[i][j].equals(matriz2[i][j]) probaste esto???
    – Yussef
    el 17 sep. 2022 a las 18:27
  • 2
    usa if Arrays.deepEquals(matriz1, matriz2){} Arrays.deepEquals() se utiliza para verificar si dos matrices de matrices unidimensionales o multidimensionales son iguales o no.
    – Dramaturgo
    el 17 sep. 2022 a las 18:32
  • @Dramaturgo, si es un ejercicio de clase para aprender a manejar arrays dudo que les dejen usar los métodos de la clase Arrays, perdería todo el sentido el ejercicio :)
    – Benito-B
    el 17 sep. 2022 a las 18:50
  • @Benito-B op hace uso del método propio de la clase Arrays, la idea de la respuesta ya se encontraba en el comentario anterior al mío aunque en vez de equals seria ==,... solo comente otra manera de resolverlo :)
    – Dramaturgo
    el 17 sep. 2022 a las 19:44

1 respuesta 1

0

En realidad deberías usar Arrays.deepEquals() ("Si ambos argumentos son matrices, el algoritmo en Arrays.deepEquals se usa para determinar la igualdad") en lugar de Arrays.equals() y no necesitas realizar otro bucle para realizar la comparación.

Scanner entrada = new Scanner (System.in);
int matriz [][] = new int [2][3];
int matriz2 [][] = new int [2][3];

System.out.println("Ingrese valores");
for (int i = 0; i < 2; i++){
    for (int j = 0; j < 3; j++){
        System.out.println("Ingrese numero primer matriz");
        matriz[i][j] = entrada.nextInt();
    }
}
for (int i = 0; i < 2 ; i++){
    for (int j = 0; j < 3; j++){
        System.out.println("Ingrese numero segunda matriz");        
        matriz2[i][j] = entrada.nextInt();
    }
}

for (int i = 0; i < 2; i++){
    System.out.println(" ");
    for (int j = 0; j < 3; j++){
        System.out.print(matriz[i][j]+" ");
            }
        }
System.out.println("");
for (int i = 0; i < 2 ; i++){
    System.out.println("");
    for (int j = 0; j < 3; j++){
        System.out.print(matriz2[i][j]+" ");
    }
}  
System.out.println("");

 //*Realiza comparación.
 if (Arrays.deepEquals(matriz, matriz2)){
    System.out.println("son iguales");     
 }else{
    System.out.println("no son iguales");
 }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.