converting a matrix to string

橙三吉。 提交于 2020-01-16 00:53:51

问题


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

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;  

 }

回答1:


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).

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.

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.




回答2:


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.

You've got a number of options:

  1. Your toString(int size1, int size2) is perfect. So when you want to print your matrix, you can go System.out.println(someMatrix.toString(2,2)) will work where someMatrix is an instance of your Matrix class.

  2. 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 your toString(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 a toString() method which can then call your toString(int size1, int size2)




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/16260739/converting-a-matrix-to-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!