帮我解决这个 Java 代码

发布于 2024-11-02 01:53:46 字数 1109 浏览 1 评论 0原文

问题

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

}

注意:除了数组 a 之外,不能使用任何额外的实例变量。

接受的答案:@Chankey:this(b1.a.length,b1.a[0].length); – 约翰

Question:

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

What should be there in the copy constructor to make a new matrix m3 of the same order as of m2?

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

}

NOTE: You can not use any extra instance variable except the array a.

Accepted answer: @Chankey: this(b1.a.length,b1.a[0].length); – John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青春有你 2024-11-09 01:53:46

将行数和列数存储在矩阵类中,并为它们创建 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;
    }


}

Store the number of rows, and the number of columns in the matrix class, and create getters for them.

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


}
椵侞 2024-11-09 01:53:46

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

int x = b1.length;
int y = b1[0].lenght;

,然后使用它来构造最终数组。

a= new int [x][y];  

You need to get the size of the passed b1 matrix

int x = b1.length;
int y = b1[0].lenght;

and can then use it to construct the final array.

a= new int [x][y];  
月下凄凉 2024-11-09 01:53:46

使用

  a =new int[b1.a.length][b1.a[0].length];

但不推荐。
你应该有一些 get 方法,它返回
矩阵维数。

Use

  a =new int[b1.a.length][b1.a[0].length];

But it is not recommended .
you should have some get method , which return
matrix dimension.

那伤。 2024-11-09 01:53:46

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

你如何获得 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];      
  }

This is homework, so I'll give you a hint:

How would you get the lengths of the 2 dimensional matrix (a[][]) of b1? proper methods in the matrix class will help - how would you implement those (getX, getY)?

Also, it is better to redirect the constructors to the most detailed one, for example:

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

 matrix(int x, int y){
  a= new int [x][y];      
  }
笨笨の傻瓜 2024-11-09 01:53:46

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

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

}

This can be the most probable answer without using any other instance variable other then the array itself:

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文