Foros del Web » Programación para mayores de 30 ;) » Java »

Duda Java básico

Estas en el tema de Duda Java básico en el foro de Java en Foros del Web. Muy buenas, no consigo entender porque no funciona el siguiente código... @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Javascript : Ver original public class Test {       ...
  #1 (permalink)  
Antiguo 03/05/2012, 02:35
 
Fecha de Ingreso: abril-2011
Mensajes: 83
Antigüedad: 13 años
Puntos: 8
Duda Java básico

Muy buenas, no consigo entender porque no funciona el siguiente código...

Código Javascript:
Ver original
  1. public class Test {
  2.    
  3.     public static double[][] copiarMatrices(double[][] origen, double[][] destino) {
  4.        
  5.         destino = new double[origen.length][origen[0].length];
  6.        
  7.         for (int x = 0; x < origen.length; x++) {
  8.             for (int y = 0; y < origen[0].length; y++) {
  9.                 destino[x][y] = origen[x][y];
  10.             }
  11.         }
  12.        
  13.         return destino;
  14.        
  15.     }
  16.  
  17.     public static void main(String[] args) {
  18.        
  19.         double[][] m = new double[][] { {10, 20, 30}, {40, 50, 60} };
  20.         double[][] m1 = copiarMatrices(m, m1);     
  21.        
  22.         for (int x = 0; x < m.length; x++)
  23.             for (int y = 0; y < m[0].length; y++)
  24.                 System.out.print(m1[x][y]);
  25.        
  26.     }
  27. }

¿No puedo inicializar el Array m1 pasándole la referencia al método?

Muchas gracias, saludos.
  #2 (permalink)  
Antiguo 03/05/2012, 12:48
 
Fecha de Ingreso: abril-2011
Mensajes: 1.342
Antigüedad: 13 años
Puntos: 344
Respuesta: Duda Java básico

Java no te permite pasar una referencia no inicializada a un método.

Tendrías que hacer esto:

Código Java:
Ver original
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package controller;
  6.  
  7. public class Test {
  8.    
  9.     public static double[][] copiarMatrices(double[][] origen, double[][] destino) {
  10.        
  11.         destino = new double[origen.length][origen[0].length];
  12.        
  13.         for (int x = 0; x < origen.length; x++) {
  14.             for (int y = 0; y < origen[0].length; y++) {
  15.                 destino[x][y] = origen[x][y];
  16.             }
  17.         }
  18.        
  19.         return destino;
  20.        
  21.     }
  22.  
  23.     public static void main(String[] args) {
  24.        
  25.         double[][] m = new double[][] { {10, 20, 30}, {40, 50, 60} };
  26.         double[][] m1 = null;
  27.        
  28.         m1 = copiarMatrices(m, m1);      
  29.        
  30.         for (int x = 0; x < m.length; x++)
  31.             for (int y = 0; y < m[0].length; y++)
  32.                 System.out.print(m1[x][y]);
  33.        
  34.     }
  35. }

Pero si te fijas, no es necesario pasarle la referencia, sólo devolver la referencia a la matriz de destino:

Código Java:
Ver original
  1. public class Test {
  2.    
  3.     public static double[][] copiarMatrices(double[][] origen) {
  4.        
  5.         double[][] destino = new double[origen.length][origen[0].length];
  6.        
  7.         for (int x = 0; x < origen.length; x++) {
  8.             for (int y = 0; y < origen[0].length; y++) {
  9.                 destino[x][y] = origen[x][y];
  10.             }
  11.         }
  12.        
  13.         return destino;
  14.        
  15.     }
  16.  
  17.     public static void main(String[] args) {
  18.        
  19.         double[][] m = new double[][] { {10, 20, 30}, {40, 50, 60} };
  20.         double[][] m1 = copiarMatrices(m);      
  21.        
  22.         for (int x = 0; x < m.length; x++)
  23.             for (int y = 0; y < m[0].length; y++)
  24.                 System.out.print(m1[x][y]);
  25.        
  26.     }
  27. }
  #3 (permalink)  
Antiguo 04/05/2012, 05:05
 
Fecha de Ingreso: abril-2011
Mensajes: 83
Antigüedad: 13 años
Puntos: 8
Respuesta: Duda Java básico

Muchas gracias :)

Etiquetas: string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:30.