Ver Mensaje Individual
  #2 (permalink)  
Antiguo 03/05/2012, 12:48
alexg88
 
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. }