如何正确地将对象显示为字符串

发布于 2024-09-28 10:04:39 字数 1097 浏览 2 评论 0原文

我正在处理这段代码并期望打印一个矩阵,但这就是出现的结果 Matrix@2c78bc3b Matrix@2a8ddc4c

这是一个代码示例:

public class Matrix
{

    public static int rows;
    public static int colms;//columns
    public static int[][] numbers;

    public Matrix(int[][] numbers)
    {

        numbers = new int[rows][colms];

    }


    public static boolean isSquareMatrix(Matrix m)
    {
        //rows = numbers.length;
        //colms = numbers[0].length;

        if(rows == colms)
           return true;
        else
            return false;
    }

    public static Matrix getTranspose(Matrix trans)
    {
       trans = new Matrix(numbers);

        for(int i =0; i < rows; i++)
        {
            for(int j = 0; j < colms; j++)
            {
                trans.numbers[i][j] = numbers[j][i];
            }
        }
        return trans;
    }



    public static void main(String[] args)
    {
        int[][] m1 = new int[][]{{1,4}, {5,3}};
        Matrix Mat = new Matrix(m1);

        System.out.print(Mat);
        System.out.print(getTranspose(Mat));

    }
}

Im working on this code and expecting a matrix to be printed but thats what came up
Matrix@2c78bc3b Matrix@2a8ddc4c

This is a code example:

public class Matrix
{

    public static int rows;
    public static int colms;//columns
    public static int[][] numbers;

    public Matrix(int[][] numbers)
    {

        numbers = new int[rows][colms];

    }


    public static boolean isSquareMatrix(Matrix m)
    {
        //rows = numbers.length;
        //colms = numbers[0].length;

        if(rows == colms)
           return true;
        else
            return false;
    }

    public static Matrix getTranspose(Matrix trans)
    {
       trans = new Matrix(numbers);

        for(int i =0; i < rows; i++)
        {
            for(int j = 0; j < colms; j++)
            {
                trans.numbers[i][j] = numbers[j][i];
            }
        }
        return trans;
    }



    public static void main(String[] args)
    {
        int[][] m1 = new int[][]{{1,4}, {5,3}};
        Matrix Mat = new Matrix(m1);

        System.out.print(Mat);
        System.out.print(getTranspose(Mat));

    }
}

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

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

发布评论

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

评论(5

心如荒岛 2024-10-05 10:04:39

您需要以有意义的方式实现toString()

这个toString()(如下)可能适合调试,但如果将其用于真实的用户输出,则会变得丑陋且令人困惑。实际的解决方案可能会使用 格式化程序以某种复杂的方式生成整齐的表格行和列。

根据您的代码的一些其他建议:

  • 建议不要单独存储行/列大小。 SSOT/单一事实来源Java +干燥。只需使用 .length,并在需要时提供访问器方法。
  • 在方法参数中使用 final ,它将消除像上面那样的错误,在构造函数中错误地别名使用 numbers
  • 使用实例,而不是 static
  • 偏执是程序员的生活方式:我还修改了代码,对提供的 int[][] 数组进行了 deepCopy,否则会出现引用泄漏,并且 如果调用者代码后来修改了它们传入的int[][],Matrix类将无法强制执行自己的不变量。

  • 我使我的Matrix不可变(请参阅最终私有数字[][])出于习惯。这是一个很好的实践,除非您为可变实现提出了充分的理由(出于矩阵中的性能原因,这并不奇怪)。

这是一些改进的代码:

public final class Matrix
{
    final private int[][] numbers;

    // note the final, which would find a bug in your cited code above...
    public Matrix(final int[][] numbers)
    {   
        // by enforcing these assumptions / invariants here, you don't need to deal 
        // with checking them in other parts of the code.  This is long enough that you might 
        // factor it out into a private void sanityCheck() method, which could be 
        // applied elsewhere when there are non-trivial mutations of the internal state

        if (numbers == null || numbers.length == 0) 
          throw new NullPointerException("Matrix can't have null contents or zero rows");
        final int columns = numbers[0].length;
        if (columns == 0) 
          throw new IllegalArgumentException("Matrix can't have zero columns");
        for (int i =1; i < numbers.length; i++) {
          if (numbers[i] == null) 
             throw new NullPointerException("Matrix can't have null row "+i);
          if (numbers[i].length != columns) 
             throw new IllegalArgumentException("Matrix can't have differing row lengths!");
        }
        this.numbers = deepCopy(numbers);
    }

    public boolean isSquareMatrix() { return rowCount() == columnCount(); }
    public int rowCount() { return numbers.length; }
    public int columnCount() {return numbers[0].length; }

    private static int[][] deepCopy(final int[][] source)
    {
       // note we ignore error cases that don't apply because of 
       // invariants in the constructor:
       assert(source != null); assert(source.length != 0);
       assert(source[0] != null); assert(source[0].length != 0);
       int[][] target = new int[source.length][source[0].length];
       for (int i = 0; i < source.length; i++) 
          target[i] = Arrays.copyOf(source[i],source[i].length);
       return target;
    }

  public Matrix getTranspose()
  {

    int[][] trans = new int[columnCount()][rowCount()];

    for (int i = 0; i < rowCount(); i++)
      for (int j = 0; j < columnCount(); j++)
        trans[i][j] = getValue(j, i);
    return new Matrix(trans);
  }

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < numbers.length; i++) 
    { 
      for (int j = 0; j < numbers[i].length; j++) 
        sb.append(' ').append(numbers[i][j]);
      sb.append('\n');
    }
    return sb.toString();
  }

  public static void main(String[] args)
  {
    final int[][] m1 = new int[][] { { 1, 4 }, { 5, 3 } };
    Matrix mat = new Matrix(m1);
    System.out.print(mat);
    System.out.print(mat.getTranspose());
  }
}

You need to implement toString() in a meaningful way.

This toString() (below) is perhaps suitable for debugging, but will be ugly and confusing if you use it for real user output. An actual solution would probably use a Formatter in some complicated way to produce neatly tabular rows and columns.

Some additional recommendations based on your code:

  • Suggest not storing the rows/columns sizes separately. SSOT / Single Source of Truth or DRY, Java+DRY. Just use the .length, and provide accessor methods if need be.
  • Use final in method args, it will eliminate bugs like you have above, aliasing numbers incorrectly int the constructor
  • Use an instance, not static
  • Paranoia is the programmer's lifestyle: I also modified my code to do a deepCopy of the provided int[][] array, otherwise there is reference leakage, and the Matrix class would be unable to enforce its own invariants if caller code later modified the int[][] they passed in.

  • I made my Matrix immutable (see final private numbers[][]) out of habit. This is a good practice, unless you come up with a good reason for a mutable implementation (wouldn't be surprising for performance reasons in matrices).

Here's some improved code:

public final class Matrix
{
    final private int[][] numbers;

    // note the final, which would find a bug in your cited code above...
    public Matrix(final int[][] numbers)
    {   
        // by enforcing these assumptions / invariants here, you don't need to deal 
        // with checking them in other parts of the code.  This is long enough that you might 
        // factor it out into a private void sanityCheck() method, which could be 
        // applied elsewhere when there are non-trivial mutations of the internal state

        if (numbers == null || numbers.length == 0) 
          throw new NullPointerException("Matrix can't have null contents or zero rows");
        final int columns = numbers[0].length;
        if (columns == 0) 
          throw new IllegalArgumentException("Matrix can't have zero columns");
        for (int i =1; i < numbers.length; i++) {
          if (numbers[i] == null) 
             throw new NullPointerException("Matrix can't have null row "+i);
          if (numbers[i].length != columns) 
             throw new IllegalArgumentException("Matrix can't have differing row lengths!");
        }
        this.numbers = deepCopy(numbers);
    }

    public boolean isSquareMatrix() { return rowCount() == columnCount(); }
    public int rowCount() { return numbers.length; }
    public int columnCount() {return numbers[0].length; }

    private static int[][] deepCopy(final int[][] source)
    {
       // note we ignore error cases that don't apply because of 
       // invariants in the constructor:
       assert(source != null); assert(source.length != 0);
       assert(source[0] != null); assert(source[0].length != 0);
       int[][] target = new int[source.length][source[0].length];
       for (int i = 0; i < source.length; i++) 
          target[i] = Arrays.copyOf(source[i],source[i].length);
       return target;
    }

  public Matrix getTranspose()
  {

    int[][] trans = new int[columnCount()][rowCount()];

    for (int i = 0; i < rowCount(); i++)
      for (int j = 0; j < columnCount(); j++)
        trans[i][j] = getValue(j, i);
    return new Matrix(trans);
  }

  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < numbers.length; i++) 
    { 
      for (int j = 0; j < numbers[i].length; j++) 
        sb.append(' ').append(numbers[i][j]);
      sb.append('\n');
    }
    return sb.toString();
  }

  public static void main(String[] args)
  {
    final int[][] m1 = new int[][] { { 1, 4 }, { 5, 3 } };
    Matrix mat = new Matrix(m1);
    System.out.print(mat);
    System.out.print(mat.getTranspose());
  }
}
回眸一遍 2024-10-05 10:04:39

对于快速而肮脏的方法:

public String toString() {
    return Arrays.deepToString(numbers);
}

在不相关的说明中,变量 rows、colms、numbers 和方法 isSquareMatrix 不应声明为静态。否则,当您进行转置时,您最终将得到两个写入同一类变量的矩阵对象。

for a quick and dirty method:

public String toString() {
    return Arrays.deepToString(numbers);
}

On an unrelated note, the variables rows, colms, numbers and the methods isSquareMatrix should not be declared as static. Otherwise, when you get a transpose, you're going to end up with two matrix objects writing to the same class variables.

ま昔日黯然 2024-10-05 10:04:39

您没有为 Matrix 类定义 toString 方法,因此当您尝试打印 Matrix 时,您会看到默认 toString 方法的结果,该方法打印对象的类,并且唯一的 ID。

You didn't define a toString method for your Matrix class, so when you try to print a Matrix you see the result of the default toString method which prints the object's class and unique id.

屋顶上的小猫咪 2024-10-05 10:04:39
System.out.print(Mat);

它将调用 Matrix 类的 toString 方法。

因此,如果您想打印矩阵,则必须重写 toString 方法

@Override
public String toString() {
    // create here a String representation of your matrix
    // ie: String myString = "1 0 0 1\n0 1 1 1\n...";
    return "String representation of my matrix";
}
System.out.print(Mat);

it will call the toString method of the Matrix class.

So, if you want to print your Matrix, you will have to override toString method

@Override
public String toString() {
    // create here a String representation of your matrix
    // ie: String myString = "1 0 0 1\n0 1 1 1\n...";
    return "String representation of my matrix";
}
睫毛溺水了 2024-10-05 10:04:39

要在可以print 时显示Matrix 类对象,您必须在类中定义toString 方法。

代码中的另一个错误是您没有设置 rowscolms 的值。因此,当您

numbers = new int[rows][colms];

在构造函数中执行此操作时,rowscolms 将始终具有默认值 0。你需要解决这个问题。然后,您必须将矩阵元素从参数 array 复制到 numbers

To display the Matrix class object when you can print on it you'll have to define the toString method in your class.

Another bug in the code it you are not setting the value of rows and colms. So when you do

numbers = new int[rows][colms];

in your constructor, rows and colms will always have their default value of 0. You need to fix that. And then you'll have to copy the matrix elements from the parameter array to numbers.

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