矩阵到字符串输出 [英] Matrix to String output

查看:125
本文介绍了矩阵到字符串输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试弄清楚如何重新定义toString方法,以便它可以显示矩阵.这是代码.

I'm currently trying to figure out how to redifine my toString method so that it will display the matrix. Here's the code..

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


}

我知道如何显示它以及如何重新定义equals方法,我觉得它来晚了,而且我想念一些愚蠢的东西.抱歉给您带来不便!

I know how to display it and how to redifine the equals method, I feel like it's just late and I'm missing something silly. Sorry for any inconvience!

对不起,我忘记指定必须将其显示为二维行x列显示.

Sorry, I forgot to specify that it had to be shown as a 2 dimensional row x column display.

现在我在重新定义equals方法时遇到了麻烦,因为这是我分配作业所必需的.我将其重写为:

Now I'm having trouble redefining the equals method, as it is required for my assignment. I rewrote it to this:

public boolean equals(Matrix that)
      {
         return this.mat == that.mat;
      }

,它仍然输出:

m1 is not equal to m2
m1 is not equal to m3

有什么简单的方法可以解决此问题吗?

Is there any easy way to fix this?

推荐答案

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

You'd have to make a nested loop for each cell in your matrix.

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

至于相等"方法,我不太确定标准是什么. 如果两个矩阵的行数和列数相同,您的应用程序是否认为它们相等?

As for the "equal" method, I'm not quite sure what is the criteria. Does your application consider two matrixes to be equal if they both have the same number of rows and cols?

P.S.

重写equal方法是一种很好的做法,但是也最好重写"hashCode"方法. 可能与您的应用无关,但这很重要.

Overriding the equal method is a very good practice, but it is a better practice to override the "hashCode" method as well. Might not be relevant to your app, but it is important.

希望会有所帮助:)

这篇关于矩阵到字符串输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆