如何正确地将对象显示为字符串
我正在处理这段代码并期望打印一个矩阵,但这就是出现的结果 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 upMatrix@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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要以有意义的方式实现
toString()
。这个toString()(如下)可能适合调试,但如果将其用于真实的用户输出,则会变得丑陋且令人困惑。实际的解决方案可能会使用
格式化程序
以某种复杂的方式生成整齐的表格行和列。根据您的代码的一些其他建议:
.length
,并在需要时提供访问器方法。final
,它将消除像上面那样的错误,在构造函数中错误地别名使用numbers
static
偏执是程序员的生活方式:我还修改了代码,对提供的
int[][]
数组进行了deepCopy
,否则会出现引用泄漏,并且如果调用者代码后来修改了它们传入的
类将无法强制执行自己的不变量。int[][]
,Matrix我使我的
Matrix
不可变(请参阅最终私有数字[][]
)出于习惯。这是一个很好的实践,除非您为可变实现提出了充分的理由(出于矩阵中的性能原因,这并不奇怪)。这是一些改进的代码:
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 aFormatter
in some complicated way to produce neatly tabular rows and columns.Some additional recommendations based on your code:
.length
, and provide accessor methods if need be.final
in method args, it will eliminate bugs like you have above, aliasingnumbers
incorrectly int the constructorstatic
Paranoia is the programmer's lifestyle: I also modified my code to do a
deepCopy
of the providedint[][]
array, otherwise there is reference leakage, and theMatrix
class would be unable to enforce its own invariants if caller code later modified theint[][]
they passed in.I made my
Matrix
immutable (seefinal 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:
对于快速而肮脏的方法:
在不相关的说明中,变量 rows、colms、numbers 和方法 isSquareMatrix 不应声明为静态。否则,当您进行转置时,您最终将得到两个写入同一类变量的矩阵对象。
for a quick and dirty method:
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.
您没有为 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 defaulttoString
method which prints the object's class and unique id.它将调用 Matrix 类的 toString 方法。
因此,如果您想打印矩阵,则必须重写 toString 方法
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
要在可以
print
时显示Matrix
类对象,您必须在类中定义toString
方法。代码中的另一个错误是您没有设置
rows
和colms
的值。因此,当您在构造函数中执行此操作时,
rows
和colms
将始终具有默认值0
。你需要解决这个问题。然后,您必须将矩阵元素从参数array
复制到numbers
。To display the
Matrix
class object when you canprint
on it you'll have to define thetoString
method in your class.Another bug in the code it you are not setting the value of
rows
andcolms
. So when you doin your constructor,
rows
andcolms
will always have their default value of0
. You need to fix that. And then you'll have to copy the matrix elements from the parameterarray
tonumbers
.