java 将矩阵转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16260739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
converting a matrix to string
提问by user2309261
I am working on writing a matrix, but unfortunately I am stuck with the output. Instead of showing a matrix, it shows me something like:
我正在编写一个矩阵,但不幸的是我被输出困住了。它没有显示矩阵,而是显示了类似的内容:
actual matrix is Matrix@512fb063
实际矩阵是Matrix@512fb063
I need to convert the matrix to a string so that the output will look like this:
我需要将矩阵转换为字符串,以便输出如下所示:
expected the matrix:
3 8 72
4 6 60
253 2 1
the code that I've written is this:
我写的代码是这样的:
import java.util.Random;
final public class Matrix {
private final int size1; // number of rows
private final int size2; // number of columns
private final int[][] data; // M-by-N array
// create size1-by-size2 matrix of 0's
public Matrix(int size1, int size2) {
this.size1 = size1;
this.size2 = size2;
data = new int[size1][size2];
}
// create matrix based on 2d array
public Matrix(int[][] data) {
size1 = data.length;
size2 = data[0].length;
this.data = new int[size1][size2];
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
this.data[i][j] = data[i][j];
}
// creates and returns a random size1-by-size1 matrix with values between 0 and 255
public String toString(int size1, int size2) {
Matrix A = new Matrix(size1, size2);
String str = " ";
final int white = 0;
final int black = 255;
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
{
A.data[i][j] = white + (int)(Math.random() * ((black ) ));
str = (A.data[i][j]+"\t"+A.data[i][j+1]);
if (i==size1 &&j==size2) str = (A.data[i][j]+"\n");
}
return str;
}
回答by Ben Ruijl
You need to override the public String toString()
function. What you are doing now is creating a new function called String toString(int size1, int size2)
.
您需要覆盖该public String toString()
功能。您现在正在做的是创建一个名为String toString(int size1, int size2)
.
Your new function is not called when writing:
编写时不会调用您的新函数:
System.out.println(myMatrix);
You could either do:
你可以这样做:
System.out.println(myMatrix.toString(2, 2));
or override the default toString()
function.
或覆盖默认toString()
功能。
So the following code should work:
所以下面的代码应该可以工作:
@Override
public String toString() {
Matrix A = new Matrix(size1, size2);
String str = " ";
final int white = 0;
final int black = 255;
for (int i = 0; i < size1; i++)
for (int j = 0; j < size2; j++)
{
A.data[i][j] = white + (int)(Math.random() * ((black ) ));
str = (A.data[i][j]+"\t"+A.data[i][j+1]);
if (i==size1 &&j==size2) str = (A.data[i][j]+"\n");
}
return str;
}
where size1
and size2
are variables in the class.
其中size1
和size2
是类中的变量。
回答by Alexandre Lavoie
You can simply do :
你可以简单地做:
@Override
public String toString()
{
return toString(size1,size2);
}
Edit : If you want to reflect the real content of your current Matrix :
编辑:如果您想反映当前矩阵的真实内容:
@Override
public String toString()
{
StringBuilder sbResult = new StringBuilder();
for(int i = 0; i < size1;i++)
{
for(int j = 0; j < size2;j++)
{
sbResult.append(A.data[i][j]);
sbResult.append("\t");
sbResult.append(A.data[i][j+1]);
if(i == size1 && j == size2)
{
sbResult.append("\n");
}
}
}
return sbResult.toString();
}
回答by micnguyen
Your output of actual matrix is Matrix@512fb063
is actually the memory address in Java that your instance of the class Matrix sits in. That's because your program doesn't know how to "print" this class - it doesn't magically know that you want a row/column representation of it.
你的输出actual matrix is Matrix@512fb063
实际上是你的 Matrix 类实例所在的 Java 内存地址。那是因为你的程序不知道如何“打印”这个类——它并不神奇地知道你想要一个行/列表示其中。
You've got a number of options:
您有多种选择:
Your
toString(int size1, int size2)
is perfect. So when you want to print your matrix, you can goSystem.out.println(someMatrix.toString(2,2))
will work wheresomeMatrix
is an instance of your Matrix class.If you want it to work properly by you just going
System.out.println(someMatrix)
then you will need to overwrite your Matrix class' toString() function. You -almost- did that in yourtoString(int size1, int size2)
function but it didn't work because it needs to match exactly the parameters, ie:toString()
should take 0 parameters. You will need to write atoString()
method which can then call yourtoString(int size1, int size2)
你的
toString(int size1, int size2)
很完美。所以当你想打印你的矩阵时,你可以去System.out.println(someMatrix.toString(2,2))
哪里someMatrix
是你的 Matrix 类的实例。如果您希望它能够正常工作,
System.out.println(someMatrix)
那么您将需要覆盖 Matrix 类的 toString() 函数。你 - 几乎 - 在你的toString(int size1, int size2)
函数中做到了,但它没有用,因为它需要完全匹配参数,即:toString()
应该采用 0 个参数。您将需要编写一个toString()
方法,然后可以调用您的toString(int size1, int size2)
回答by Jan Gatting
Somehow you get the hashcode. Maybe you can use http://math.nist.gov/javanumerics/jama/doc/matrix implementation. I think this line is not working str = (A.data[i][j]+"\t"+A.data[i][j+1]); Don't you get an IndexOutOfBoundexception? Anyway A.data[i][j+1] is always empty within the loop. By the way, Variables in Java are always lower case.
不知何故,你得到了哈希码。也许您可以使用http://math.nist.gov/javanumerics/jama/doc/矩阵实现。我认为这条线不起作用 str = (A.data[i][j]+"\t"+A.data[i][j+1]); 你没有得到一个 IndexOutOfBoundexception 吗?无论如何 A.data[i][j+1] 在循环中总是空的。顺便说一下,Java 中的变量总是小写的。