0

Hello I am developing a mathematical library for java I wrote methods Matrix(), setMatrix (), getMatrix () is required to getmatrix() method returned the entire two-dimensional array more precisely all its values

static int[][] getMatrix(){//return matrix
        return matrix;
}//why returns a reference instead of an array value ? [[I@15db9742 only this
class Mathematik {

    private static int[][] matrix;
    private static int line_0;
    private static int column_0;

    static int Matrix(int line, int column){//Matrix
        for(int l=0; l<line; l++){//line
            for(int j=0; j<column; j++){//column
                line_0=l;
                column_0=j;
                matrix=new int[l][j];
            }
        }
        return matrix;
    }

    static int setMatrix(int num){//fill matrix
        for(int l=0; l<line_0; l++){//line
            for(int j=0; j<column_0; j++){//column
                matrix[l][j]=num;
            }
        }
        return 0;
    }

    static int[][] getMatrix(){//return matrix
        return matrix;
    }//why returns a reference instead of an array value ? [[I@15db9742 only this
}

class Activity{
    Mathematik A=new Mathematik();//call the class

    public static void main(Strin[] args){
        A.Matrix(3,3)//create matrix
        A.setMatrix(10)//to fill in the matrix

        System.out.println(""+A.getMatrix());//Writes the grid values to a string
    }
}

arrays [1a, 2a, 3a.....a]

the method getMatrix() is required to return all values of the array

4
  • Are you asking how to print a matrix? Why your code doesn't compile? What is your question? Commented Apr 19, 2019 at 13:48
  • 1
    As a beginner you should not use the word static to declare fields. Commented Apr 19, 2019 at 13:53
  • Why are you creating a new matrix array inside your nested for loops in your Matrix method? That should be created outside your nested loops. And I'm not certain what that method is supposed to do. Commented Apr 19, 2019 at 13:54
  • @jim829б to set the dimension of the matrix Commented Apr 19, 2019 at 14:06

2 Answers 2

1

It does return the matrix. You can't print the matrix like you are trying. You need to print each value. Or you can simply iterate over the rows and then pass each one to Arrays.toString() to display them. You may also want to write your own display routine for your library.

Sign up to request clarification or add additional context in comments.

5 Comments

@JB Nizet how to translate a string back into a two-dimensional array ?
I don't understand your question. The purpose of the Arrays.toString() is to display each row of the matrix. No translation is needed.
@JB Nizet wrote the method summation of the matrix rows translate into values using the method Integer.parseInt(String); writes a java error.lang.NumberFormatException: For input string: "[[50], [50], [50]]" Zero
The Arrays.toString() method for int[] is not designed to be converted to type int. It is simply used to display a list of comma separated values between square braces. And Integer.parseInt(String) only takes a single string of digits and nothing else. e.g. "1234".
Please check the Java API or Java Tutorials on how to use Arrays.toString(), Integer.parse() or any other method.
0

Because you are requiring the array and that what is returning is correct . You must convert that array to String .Try using Arrays.toString(matrix);

The current value that is getting returned is the representation in memory for the matrix value .

Please refer to this link as JB Nizet mentions

7 Comments

And it doesn't return a "memory allocation". It returns a reference to the matrix.
I am updating the answer with your name and the provided link
yes , what I am saying is that it is the reprezentation of the matrix object in memory . am I wright ?
The hashCode in the Oracle version of Java defaults to the memory address of the object. The default toString() method returns this in hex.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.