【问题标题】:How to pass array values through a contructor如何通过构造函数传递数组值
【发布时间】:2015-04-19 19:22:06
【问题描述】:

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

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");
             }
          }
       }
    }

【问题讨论】:

  • 您能澄清一下您的问题吗?我没有完全理解它。
  • 我希望能够获取分配给数组的输入值并将它们传递给另一个类进行计算。我希望我的主要方法仅用于调用返回值而不处理计算。希望有帮助
  • “类”不返回值。您不会“从数组”传递值。请澄清,最好提供方法签名。
  • 一个“类”可以有返回值的方法,我只是想弄清楚如何将值传递给构造函数。在我的代码中,用户将输入值。我想将这些值传递给另一个类中的另一个方法。我知道怎么称呼它们,但设置它们是个问题。
  • @laune 他的问题非常容易理解。我不明白你的困惑和TejjD的困惑

标签: java arrays matrix


【解决方案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");
          }
       }
    }
 }

【讨论】:

    【解决方案2】:

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

    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());
    
    }
    

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 非常有帮助。
      • 相当模糊。 size 变量来自哪里?详细说明需要做什么的答案描述并不简单。请改进答案。另外,OP想要一个构造函数,你的构造函数在哪里?
      • 好吧,我认为我不必那么明确,真的有必要解释大小的来源吗?他的问题的关键是如何将数组作为参数传递,喜欢与否就是这个非常简短的代码所解释的......
      • 啊,我现在明白你的推理了。我知道size 的来源,似乎因为OP 不理解将数组作为参数传递,他可能需要更明确的解释,因此我的答案有26 页长的文本。 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2018-07-06
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 2012-03-19
      • 1970-01-01
      相关资源
      最近更新 更多