elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Entrar al Canal Oficial Telegram de elhacker.net


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  Suma de dos matrices - Me devuelve "[[D@7d4991ad"
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Suma de dos matrices - Me devuelve "[[D@7d4991ad"  (Leído 2,062 veces)
danissj

Desconectado Desconectado

Mensajes: 6


Ver Perfil
Suma de dos matrices - Me devuelve "[[D@7d4991ad"
« en: 31 Marzo 2019, 23:54 pm »

Hice un intento de programa para sumar dos matrices (soy novato en java) , el problema es que el resultado me devuelve un "[[D@7d4991ad" y no se como podria solucionarlo. cualquier ayuda es bienvenida.

Código
  1. package bidimensional;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Bidimensional {
  6.  
  7. private double[][] matriz;
  8. private int renglones;
  9. private int columnas;
  10.  
  11. public Bidimensional(int renglones, int columnas) { // constructor y pido el ingreso de valores a matrices
  12. Scanner entrada = new Scanner(System.in);
  13. this.matriz = new double [renglones][columnas];
  14.  
  15. for(int i=0 ; i<renglones; i++)
  16. {
  17. for(int j=0; j<columnas; j++) {
  18. System.out.print("Ingresar el numero de matriz ["+i+":"+j+"]: ");
  19. matriz[i][j]=entrada.nextDouble();
  20. }
  21. }
  22. }
  23.  
  24. public double[][] suma(Bidimensional m2) // operacion de suma
  25. {
  26. for(int i=0; i<2; i++) {
  27. for(int j=0; j<2; j++)
  28. {
  29. matriz[i][j]=matriz[i][j]+m2.matriz[i][j];
  30. }
  31. }
  32.  
  33. return matriz;
  34. }
  35.  
  36.  
  37. }

MAIN:

Código
  1. package testBidimensional;
  2.  
  3. import bidimensional.Bidimensional;
  4.  
  5. public class TestBidimensional {
  6.  
  7. public static void main(String[] args) {
  8. // TODO Auto-generated method stub
  9.  
  10. Bidimensional matrices1 = new Bidimensional(2,2);
  11. Bidimensional matrices2 = new Bidimensional(2,2);
  12.  
  13. System.out.println("Suma: "+matrices1.suma(matrices2));
  14.  
  15. }
  16.  
  17. }


En línea

rub'n


Desconectado Desconectado

Mensajes: 1.217


(e -> λ("live now")); tatuar -> λ("α");


Ver Perfil WWW
Re: Suma de dos matrices - Me devuelve "[[D@7d4991ad"
« Respuesta #1 en: 1 Abril 2019, 00:30 am »

Casi casi lo tiene dog  :xD, recorrer con doble ciclo for como haces con el método suma, porque debes recorrer cada indice, me da aburrimiento máximo llenarla, pero aleatoria nom y así ahorras tiempo en desarrollo, no apto para produccion  :xD

Pero me imagino que entiendes no? ejemplo las coordenada [0,0] de la matris1 se sumara con la [0,0] de la matris 2 y asi

Código
  1. public class Bidimensional {
  2.  
  3. private double[][] matriz;
  4. private int renglones;
  5. private int columnas;
  6. private static final SecureRandom SR = new SecureRandom();
  7. private static final DecimalFormat TWO_DIGITS = new DecimalFormat("#.##");
  8.  
  9. public Bidimensional(int renglones, int columnas) { // constructor y pido el ingreso de valores a matrices
  10. Scanner entrada = new Scanner(System.in);
  11. this.matriz = new double [renglones][columnas];
  12.  
  13. for(int i=0 ; i<renglones; i++)
  14. {
  15. for(int j=0; j<columnas; j++) {
  16. matriz[i][j]= Double.valueOf(TWO_DIGITS.format(1 + SR.nextDouble()));
  17. System.out.print("Ingresar el numero de matriz ["+i+":"+j+"]: ");
  18. System.out.println(matriz[i][j]);
  19.  
  20. }
  21. }
  22. }
  23.  
  24. public double[][] suma(Bidimensional m2) // operacion de suma
  25. {
  26. for(int i=0; i<2; i++) {
  27. for(int j=0; j<2; j++)
  28. {
  29. matriz[i][j]= matriz[i][j] + m2.matriz[i][j];
  30. }
  31. }
  32.  
  33. return matriz;
  34. }
  35.  
  36.        public double format(final double num) {
  37.            return Double.valueOf(TWO_DIGITS.format(num).replace(",", "."));
  38.        }
  39.  
  40. public static void main(String ...blalba) {
  41.  
  42. final Bidimensional matris = new Bidimensional(2,2);
  43. final Bidimensional matris2 = new Bidimensional(2,2);
  44.  
  45. final double[][] resultado = matris.suma(matris2);
  46. //Recorrer el Array de doble dimension, con java 8, java 7 usa doble for
  47. Arrays.stream(resultado)
  48. .flatMapToDouble(Arrays::stream)
  49.                                .map(this::format)
  50. .forEach(System.out::println);
  51.  
  52. }
  53. }

Código
  1. Ingresar el numero de matriz [0:0]: 1.32
  2. Ingresar el numero de matriz [0:1]: 1.8
  3. Ingresar el numero de matriz [1:0]: 1.62
  4. Ingresar el numero de matriz [1:1]: 1.71
  5. Ingresar el numero de matriz [0:0]: 1.53
  6. Ingresar el numero de matriz [0:1]: 1.6
  7. Ingresar el numero de matriz [1:0]: 1.6
  8. Ingresar el numero de matriz [1:1]: 1.57
  9. 2.85
  10. 3.4
  11. 3.22
  12. 3.28
  13.  
  14. Process finished with exit code 0

Tambien te sirve asi, y cualquier otra manera que funcione XD

Código
  1. public double[][] suma(Bidimensional m2) // operacion de suma
  2. {
  3. for(int i=0; i<matriz.length; i++) {
  4. for(int j=0; j<m2.matriz[i].length; j++)
  5. {
  6. matriz[i][j]= matriz[i][j] + m2.matriz[i][j];
  7. }
  8. }
  9.    return matriz;
  10. }

Y asi


Código
  1. for(int i=0; i<matriz.length; i++) {
  2.  for(int j=0; j<matriz[i].length; j++) {
  3. matriz[i][j]= matriz[i][j] + m2.matriz[i][j];
  4.  }
  5. }


« Última modificación: 5 Abril 2019, 08:40 am por rub'n » En línea



rubn0x52.com KNOWLEDGE  SHOULD BE FREE.
If you don't have time to read, you don't have the time (or the tools) to write, Simple as that. Stephen ki
CalgaryCorpus


Desconectado Desconectado

Mensajes: 323


Ver Perfil WWW
Re: Suma de dos matrices - Me devuelve "[[D@7d4991ad"
« Respuesta #2 en: 1 Abril 2019, 05:10 am »

Una alternativa a esto es que el metodo suma no devuelva nada, o bien devuelva una referencia hacia el objeto Bidimensional y que sea el metodo toString() el que imprima los valores al interior del objeto Bidimensional.
En línea

Aqui mi perfil en LinkedIn, invitame un cafe aqui
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines