如何通过构造函数传递数组值

时间:2015-04-19 19:22:07

标签: java arrays matrix

我所拥有的代码将在数组中乘以两个矩阵并输出该值。我接下来要做的是将代码分成两类。我想要一个可以接受用户输入并将数组值传递给另一个类中的构造函数然后计算相同值的类。我知道如何从第二个类中获取返回类型,但不知道如何从数组中传递值进行计算。任何帮助,将不胜感激。谢谢!!

import java.util.Scanner;

class MatrixApp
{
   public static void main(String args[])
   {
      int m, n, p, q; 
      int sum = 0, c, d, k;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of first matrix");
      m = in.nextInt();
      n = in.nextInt();

      int first[][] = new int[m][n];

      System.out.println("Enter the elements of first matrix");

      for ( c = 0 ; c < m ; c++ )
         for ( d = 0 ; d < n ; d++ )
            first[c][d] = in.nextInt();

      System.out.println("Enter the number of rows and columns of second matrix");
      p = in.nextInt();
      q = in.nextInt();

      if ( n != p )
         System.out.println("Matrices with entered orders can't be    multiplied with each other.");
      else
      {
         int second[][] = new int[p][q];
         int multiply[][] = new int[m][q];

         System.out.println("Enter the elements of second matrix");

         for ( c = 0 ; c < p ; c++ )
            for ( d = 0 ; d < q ; d++ )
               second[c][d] = in.nextInt();

         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
            {   
               for ( k = 0 ; k < p ; k++ )
               {
                  sum = sum + first[c][k]*second[k][d];
               }

               multiply[c][d] = sum;
               sum = 0;
            }
         }

         System.out.println("Product of entered matrices:-");

         for ( c = 0 ; c < m ; c++ )
         {
            for ( d = 0 ; d < q ; d++ )
               System.out.print(multiply[c][d]+"\t");

                System.out.print("\n");
             }
          }
       }
    }

3 个答案:

答案 0 :(得分:1)

我不确定我是否理解正确,但是如果你想将数组值传递给构造函数并进行计算,那么下面是一个这样的实现,我根据你的需要使用了两个类。 这是一个非常快速的实现,所以我没有优化它。

import java.util.Scanner;

公共类TestMatrixApp {

/**
 * @param args
 */
public static void main(String[] args) {
int m, n, p, q;

Scanner input = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of first matrix");
m = input.nextInt();
n = input.nextInt();

System.out
    .println("Enter the number of rows and columns of second matrix");
p = input.nextInt();
q = input.nextInt();

System.out.println("Enter the elements of first matrix");

int first[][] = new int[m][n];
for (int c = 0; c < m; c++)
    for (int d = 0; d < n; d++)
    first[c][d] = input.nextInt();

System.out.println("Enter the elements of second matrix");
int second[][] = new int[p][q];

for (int c = 0; c < p; c++)
    for (int d = 0; d < q; d++)
    second[c][d] = input.nextInt();

MatrixApp matrixApp = new MatrixApp(first, second, m, n, p, q);
}

}

下一堂课

公共类MatrixApp {

public MatrixApp(int first[][], int second[][], int m, int n, int p, int q) {
doMatrixMultiply(first, second, m, n, p, q);
}

public void doMatrixMultiply(int first[][], int second[][], int m, int n, int p, int q)
{

   if ( n != p )
      System.out.println("Matrices with entered orders can't be    multiplied with each other.");
   else
   {
      int multiply[][] = new int[m][q];
      int sum = 0;
      for ( int c = 0 ; c < m ; c++ )
      {
         for (int d = 0 ; d < q ; d++ )
         {   
            for (int k = 0 ; k < p ; k++ )
            {
               sum = sum + first[c][k]*second[k][d];
            }

            multiply[c][d] = sum;
            sum = 0;
         }
      }

      System.out.println("Product of entered matrices:-");

      for (int c = 0 ; c < m ; c++ )
      {
         for (int d = 0 ; d < q ; d++ )
            System.out.print(multiply[c][d]+"\t");

             System.out.print("\n");
          }
       }
    }
 }

答案 1 :(得分:1)

你真正需要的是一个拥有所有代码的类,无论你想要它们有什么代码,尽管你真的不需要两个类。处理计算的类实际上只需要看起来像这样的构造函数。参数是整数(原始数据)矩阵。请记住,包含此构造函数的类必须有两个私有变量:

private int[][] matrixMult, int[][] matrixMult2;//private variables

public classObject(int matrixName[][], int matrixName2[][])
{
    matrixMult = matrixName;
    matrixMult2 = matrixName2;
}

然后它需要一个你似乎已经失效的计算方法。然后,您只需使用main方法打开一个类,让用户输入两个矩阵的矩阵值,然后将它们放入构造函数中。你可以像likeToCode那样做,并让它在构造函数中进行数学运算,或者你可以在用上面的构造函数构造之后调用乘法方法。另请注意,我向您展示的构造函数只显示了如何将矩阵放入构造函数中,您需要添加所需的其他值。

public static void main(String[] args)
{
    //create and fill matrices
    classObject bOB = new classObject(matrixOne, matrixTwo);//add on other values as needed
    System.out.println(bOB.multMatrices());

}

在这里,您创建了所需的第二个类的对象,并完成了乘法运算。我不明白为什么你想要两个课程,但在你的情况下它真的很简单。

答案 2 :(得分:0)

您需要在构造函数中使用[]声明数组,并在没有它们的情况下调用它,例如:

void receiveArray(int[] array){
   //doStuff
}

private void callerMethod(){
   . . .
   int[] array = new int[size];
   //Populate
   receiveArray(array);
}