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

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

最佳答案

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

import java.util.Scanner;

公共(public)类 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);
}

}

下一个类

公共(public)类 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");
          }
       }
    }
 }

关于java - 如何通过构造函数传递数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29734687/

相关文章:

matlab - 在MATLAB中绘制一系列2D黑白图

c++ - 计算行列式的最快方法是什么?

java - 在 Twilio 发起的通话期间发送短信或调用其他人

java - 安卓 Java : Extract substring from uri string after particular characters

java - java拆分类时spring失败

javascript - React 将对象插入数组,始终插入最后一个数组

python - 如何在 numpy 矩阵中使用这个复数?

java - 跨应用程序共享 Java 代码的最佳方式是什么

javascript - 尝试使用基于数组的jquery输出单独的点击函数

python - numpy 数组连接错误 : 0-d arrays can't be concatenated