2012-10-31 3 views
2
public class DoubleMatrix 
{ 
    private double[][] doubMatrix; 

    public DoubleMatrix(int row, int col) 
    {    
      if(row > 0 && col > 0) 
      { 
        makeDoubMatrix(row,col); 
      } 
      else 
      { 
        row = 1; 
        col = 1; 
      } 
    } 

    public DoubleMatrix(double[][] tempArray) 
    { 
      if(tempArray != null) 
      { 
        for(int i = 0; i < tempArray.length-1;i++) 
        { 
          if(tempArray[i].length == tempArray[i+1].length) 
          { 
            tempArray = doubMatrix; 
          } 
        } 
      } 
      else 
      { 
        makeDoubMatrix(1,1); 
      } 
    } 

    public int getDim1() 
    { 
      return doubMatrix.length; 
    } 

    public int getDim2() 
    { 
      return doubMatrix[0].length; 
    } 

    private void makeDoubMatrix(int row, int col) 
    { 
     double[][] tempArray = new double[row][col]; 
     for (int i = 0;i < row;i++) 
     { 
      for(int j = 0;j < col;j++) 
      { 
       tempArray[i][j] = Math.random() * (100);    
       doubMatrix[i][j] = tempArray[i][j]; 
     } 
     } 
    } 
    public DoubleMatrix addMatrix(DoubleMatrix secondMatrix) 
    {  
      //this. doubMatrix = doubMatrix; 
      double[][] tempArray; 
      if(secondMatrix.doubMatrix.length == doubMatrix.length) 
        if(secondMatrix.doubMatrix[0].length == doubMatrix[0].length) 
        { 
          tempArray = new double[doubMatrix.length][doubMatrix[0].length]; 
          for(int i = 0; i< secondMatrix.doubMatrix.length;i++) 
            for(int j = 0; j< secondMatrix.doubMatrix[i].length;j++) 
            { 
             tempArray[i][j] = secondMatrix.doubMatrix[i][j] + doubMatrix[i][j];// add two matrices 
            }//end for  
          return new DoubleMatrix (tempArray); 
        } 


          return new DoubleMatrix(1,1);                 
    } 


    public DoubleMatrix getTransposedMatrix() 
    { 
      double[][] tempArray = new double[doubMatrix.length][doubMatrix[0].length]; 
      for(int i = 0;i < doubMatrix.length;i++) 
      for(int j = 0;j < doubMatrix[i].length;j++) 
      { 
       tempArray[j][i] = doubMatrix[i][j];// transposed matrix2   
      }//end for   
      return new DoubleMatrix(tempArray); 
    } 

    public DoubleMatrix multiplyingMatrix(DoubleMatrix secondMatrix) 
    { 
      double[][] tempArray = new double[secondMatrix.doubMatrix.length][doubMatrix[0].length]; 
      //check if dimension of matrix1 equal to dimension of matrix2 
      if(secondMatrix.doubMatrix[0].length == doubMatrix.length) 
        if(doubMatrix.length == secondMatrix.doubMatrix[0].length) 
      { 

          for (int i = 0; i <secondMatrix.doubMatrix.length; i++) 
        { 
         for(int j = 0; j < doubMatrix[0].length; j++) 
         { 

          for (int k = 0; k < doubMatrix.length; k++) 
          { 
           tempArray[i][j] = tempArray[i][j] + secondMatrix.doubMatrix[i][k]*doubMatrix[k][j]; // multiply 2 matrices 

          } 
         } 
        }//end for 
      }// end if 



          return new DoubleMatrix(1,1);         
    } 

    public void printMatrix(String text) 
    { 
      System.out.println(text);// output string 
      for(int i = 0; i< doubMatrix.length;i++) 
      { 
        for(int j = 0; j< doubMatrix[i].length;j++)  {         
         System.out.printf("%9.1f", doubMatrix[i][j]);// out put value for matrices     
        } 
        System.out.println(); 
      } 
    } 
} 

public class Program3 
{ 
public static void main(String[] args) 
{ 
     int num1 = (int) (Math.random()*(10-3+1)+3); 
     int num2 = (int) (Math.random()*(10-3+1)+3); 
     DoubleMatrix doubMatObj1 = new DoubleMatrix(num1,num2); 
     DoubleMatrix doubMatObj2 = new DoubleMatrix(doubMatObj1.getDim1(),doubMatObj1.getDim2()); 
     DoubleMatrix doubMatObj3; 


     doubMatObj2.getDim1(); 

     doubMatObj3 = doubMatObj1.addMatrix(doubMatObj2); 
     doubMatObj1.printMatrix("First Matrix Object"); 
     doubMatObj2.printMatrix("Second Matrix Object"); 
     doubMatObj3.printMatrix("Result of Adding Matrix Objects"); 
     doubMatObj2 = doubMatObj2.getTransposedMatrix(); 
     doubMatObj2.printMatrix("Result of inverting Matrix Object"); 
     doubMatObj3 = doubMatObj1.multiplyingMatrix(doubMatObj2); 
     doubMatObj3.printMatrix("Result of Multiplying Matrix Objects"); 
} 
} 

Salut, j'ai une erreur NullPointerException dans la dernière déclaration de ligne de la méthode makeDoubMatrix ainsi l'makedoubMatrix d'appel à côté si la déclaration du premier constructeur. Doubematrix semble être nul quand je l'ai déjà initialisé. Comment vais-je pouvoir résoudre ce problème?tableau 2D pointeur nul d'erreur d'exception

Répondre

4

Vous voulez initialiser le tableau, i.e. .:

private double[][] doubMatrix = new double[size1][size2]; 

où taille1 et size2 sont des tailles arbitraires. Ce que vous voulez sans doute est:

if(row > 0 && col > 0) 
{ 
    doubMatrix = new double[row][col]; 
    makeDoubMatrix(row,col); 
} 
else 
{ 
    doubMatrix = new double[1][1]; 
    makeDoubMatrix(1,1); 
} 

qui initialise le tableau doubMatrix à une taille de la ligne * col si les deux rangs et col sont greather que 0 et 1 * 1 sinon, puis appelle makeDoubMatrix avec sa taille initialisés (vous pourriez avoir cette méthode appel après l'if-else, en utilisant doubMatrix.size et doubMatrix[0].size, mais je pense que c'est plus lisible maintenant).

Modifiez le second constructeur (qui prend un tableau 2D) en utilisant le même raisonnement.

1

Vous êtes et non en cours d'initialisation doubMatrix. La seule ligne qui attribue une valeur à doubMatrix est commenté ce un:

//this. doubMatrix = doubMatrix; 

(Et ce ne serait pas utile.)

Demandez-vous où vous pensez vous initialisant - où faire vous pensez que vous avez quelque chose comme:

doubMatrix = new double[1][2]; 

... ou d'une cession copie d'une valeur d'un autre tableau:

doubMatrix = someOtherVariable; 

Si vous n'avez pas des déclarations en lui attribuant une valeur, vous ne sont pas initialisant, il aura toujours la valeur par défaut de null.