Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
请帮助我了解这个Java代码_Java_Copy Constructor - Fatal编程技术网

请帮助我了解这个Java代码

请帮助我了解这个Java代码,java,copy-constructor,Java,Copy Constructor,问题: matrix m1 = new matrix(); // should produce a matrix of 3*3 matrix m2 = new matrix(5,4); //5*4 matrix m3 = new matrix(m2); //5*4 复制构造函数中应该有什么来生成与m2相同顺序的新矩阵m3 public class matrix { int a[ ][ ]; matrix(){ a = new int[3][3];

问题

matrix m1 = new matrix(); // should produce a matrix of 3*3

matrix m2 = new matrix(5,4); //5*4

matrix m3 = new matrix(m2); //5*4
复制构造函数中应该有什么来生成与m2相同顺序的新矩阵m3

 public class matrix {

    int a[ ][ ];

       matrix(){
        a = new int[3][3];  
      }

     matrix(int x, int y){
        a= new int [x][y];      
      }

     matrix (matrix b1){        
      //how to use value of x and y here....
      }

void show(){

        System.out.println(a.length);
        for(int i=0;i<a.length;i++){
            System.out.print(a[i].length);      
            }
        } 
     }




public class matrixtest { 

public static void main(String [ ] args){   

       matrix a = new matrix();     
       matrix b = new matrix(5,4);  
       matrix c  = new matrix (b);  
       a.show(); 

       b.show(); 

       c.show(); 
   } 

}
公共类矩阵{
INTA[][];
矩阵(){
a=新整数[3][3];
}
矩阵(整数x,整数y){
a=新整数[x][y];
}
矩阵(矩阵b1){
//这里如何使用x和y的值。。。。
}
无效显示(){
系统输出打印长度(a.长度);

对于(int i=0;i

您需要获得传递的
b1
矩阵的大小

int x = b1.length;
int y = b1[0].lenght;
然后可以使用它来构造最终的数组

a= new int [x][y];  

存储矩阵类中的行数和列数,并为它们创建getter

public class Matrix {
    int[][] a;
    int rowNum;
    int colNum;

    //...
    public Matrix(Matrix b) {
       a=new int[b.getRowNum()][b.getColNum()];
       this.rowNum = b.getRowNum();
       this.colNum = b.getColNum();
    }

    public int getRowNum() {
       return this.rowNum;
    }


}
使用

但不建议这样做。 您应该有一些get方法,返回
矩阵维数。

这是家庭作业,所以我给你一个提示:

如何获得b1的二维矩阵(
a[][]
)的长度?矩阵类中的适当方法将有所帮助-如何实现这些(getX,getY)

此外,最好将构造函数重定向到最详细的构造函数,例如:

matrix(){
    this(3,3);  // call the constructor below with parameters 3,3
  }

 matrix(int x, int y){
  a= new int [x][y];      
  }

这可能是最可能的答案,无需使用除数组本身以外的任何其他实例变量:

import java.io.*;
class matrix
{
private int arr[][];
public matrix() //Default Constructor
{
    this(3,3);
}

public matrix(int r,int c) //Parameterized Constructor
{
    arr=new int[r][c];
    read();
}

public matrix(matrix m)  //Copy Constructor
{
    System.out.println("Fetching array...");
    int r,c;
    r=m.arr.length;
    c=m.arr[0].length;
    arr=new int [r][c];
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            arr[i][j]=m.arr[i][j];
        }
    }
}

public void read()
{
    int i,j,r,c;
    r=arr.length;
    c=arr[0].length;
    Console con=System.console();
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            arr[i][j]=Integer.parseInt(con.readLine());
        }
    }
}
public void show()
{
    int i,j;
    for(i=0;i<arr.length;i++)
    {
        for(j=0;j<arr[0].length;j++)
        {
            System.out.print(" "+arr[i][j]);
        }
        System.out.println();
    }
}
import java.io.*;
类矩阵
{
私有内部arr[][];
public matrix()//默认构造函数
{
这(3,3);
}
公共矩阵(int r,int c)//参数化构造函数
{
arr=新整数[r][c];
read();
}
公共矩阵(矩阵m)//复制构造函数
{
System.out.println(“获取数组…”);
int r,c;
r=m.arr.length;
c=m.arr[0]。长度;
arr=新整数[r][c];

对于(int i=0;iuse a=new int[b1.a.length][b1.a[0.length];
this(b1.a.length,b1.a[0.length);
只需添加,您的类名应该以大写字母开头。b1没有长度属性,它不是数组。+1表示面向对象和封装的答案(不直接访问
b.a
)!谢谢,但我知道这个方法。我被要求在不使用任何实例变量的情况下解决问题。John已经在评论中给出了解决方案,但我认为我也应该接受你的答案。:)
import java.io.*;
class matrix
{
private int arr[][];
public matrix() //Default Constructor
{
    this(3,3);
}

public matrix(int r,int c) //Parameterized Constructor
{
    arr=new int[r][c];
    read();
}

public matrix(matrix m)  //Copy Constructor
{
    System.out.println("Fetching array...");
    int r,c;
    r=m.arr.length;
    c=m.arr[0].length;
    arr=new int [r][c];
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
        {
            arr[i][j]=m.arr[i][j];
        }
    }
}

public void read()
{
    int i,j,r,c;
    r=arr.length;
    c=arr[0].length;
    Console con=System.console();
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            arr[i][j]=Integer.parseInt(con.readLine());
        }
    }
}
public void show()
{
    int i,j;
    for(i=0;i<arr.length;i++)
    {
        for(j=0;j<arr[0].length;j++)
        {
            System.out.print(" "+arr[i][j]);
        }
        System.out.println();
    }
}