Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.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_String_Matrix - Fatal编程技术网

Java 矩阵到字符串输出

Java 矩阵到字符串输出,java,string,matrix,Java,String,Matrix,我目前正试图找出如何重新定义我的toString方法,以便它显示矩阵。这是密码 import java.util.Random; public class TextLab09st { public static void main(String args[]) { System.out.println("TextLab09\n\n"); Matrix m1 = new Matrix(3,4,1234); Matrix m

我目前正试图找出如何重新定义我的toString方法,以便它显示矩阵。这是密码

    import java.util.Random;


public class TextLab09st
{
    public static void main(String args[])
    {
        System.out.println("TextLab09\n\n");
        Matrix m1 = new Matrix(3,4,1234);
        Matrix m2 = new Matrix(3,4,1234);
        Matrix m3 = new Matrix(3,4,4321);
        System.out.println("Matrix m1\n");
        System.out.println(m1+"\n\n");
        System.out.println("Matrix m2\n");
        System.out.println(m2+"\n\n");
        System.out.println("Matrix m3\n");
        System.out.println(m3+"\n\n");
        if (m1.equals(m2))
            System.out.println("m1 is equal to m2\n");
        else
            System.out.println("m1 is not equal to m2\n");
        if (m1.equals(m3))
            System.out.println("m1 is equal to m3\n");
        else
            System.out.println("m1 is not equal to m3\n");
    }
}


class Matrix
{
    private int rows;
    private int cols;
    private int mat[][];

    public Matrix(int rows, int cols, int seed)
    {
        this.rows = rows;
        this.cols = cols;
        mat = new int[rows][cols];
        Random rnd = new Random(seed);
        for (int r = 0; r < rows; r ++)
            for (int c = 0; c < cols; c++)
            {
                int randomInt = rnd.nextInt(90) + 10;
                mat[r][c] = randomInt;
            }
    }
    public String toString()
    {

        return ("[" + mat + "]");
    }
    public boolean equals(Matrix that)
     {
         return this.rows == (that.rows)&&this.cols == that.cols;
     }


}
它仍然输出:

m1 is not equal to m2
m1 is not equal to m3
有什么简单的方法可以解决这个问题吗?

使用:


您必须为矩阵中的每个单元格创建嵌套循环

public String toString()
{

    String str = "";

    for (int i = 0 ; i<this.rows ; i ++ ){
        for (int j = 0 ; j < this.cols ; j++){
            str += mat[i][j]+"\t";
        }
        str += "\n";
    }
    return str;
}
公共字符串toString()
{
字符串str=“”;

对于(int i=0;i您当前的输出是什么?您想要什么?我当前的输出只是矩阵的内存地址。我需要它以二维方式显示(行x列)对于equals,你应该检查rows和cols countr是否相同,然后你可以检查数组的元素是否相同。你可以使用to string方法中的double for循环来访问每个元素,看看它们是否相同。我很累,我一直在尝试按照你说的做,但我似乎无法检查数组中的元素是相同的。有什么建议吗?我忘了提到的另一件事是,重写equal方法是赋值的一部分。是的,它会检查两个矩阵的行数和列数是否相似。我现在意识到,这与我试图通过重写equal方法来完成的任务无关方法。现在我在编写重写equals方法的代码时也遇到了麻烦,因为我似乎不知道如何使用rows和cols变量以外的其他变量来编写它。任何提示或帮助都将不胜感激!事实上,将rows和cols的数量保留为类成员是redandent。Instrain您可以添加2个方法来检索列数和行数以及在equals方法中使用它们的方法如下:public int numorrows(){return this.mat.length;}public int numorfs(){if(this.numorrows()>0)return this.mat[0].length;else返回0;}public boolean equals(矩阵){返回this.numorrows()==(that.numorrows())&&this.numorfs()==that.numorfs();}
public String toString()
{
    return Arrays.deepToString(mat);
}
public String toString()
{

    String str = "";

    for (int i = 0 ; i<this.rows ; i ++ ){
        for (int j = 0 ; j < this.cols ; j++){
            str += mat[i][j]+"\t";
        }
        str += "\n";
    }
    return str;
}