1

I create a class called MATRIX which has one attribute

int [][] _matrix;

In another class I create an obj of type MATRIX:

Matrix newMatrix=new Matrix();

I want to initialize the obj like in this example:

int [][] a={{1,2,3}, {2,2,4}}

I try to write

newMatrix._matrix={{1,2,3}, {2,2,4}}

and I got a syntax error.

What did I've done wrong?

Jasurbek
  • 2,946
  • 3
  • 20
  • 37
  • 1
    Welcome to Stack Overflow! Please consider to provide a [Minimal, Reproducible Example](http://stackoverflow.com/help/minimal-reproducible-example) if possible. This way, it's more likely volunteers on SO can help you. – lealceldeiro Jun 18 '19 at 12:19
  • note: `int [][] a={{1,2,3}, {2,2,4}}` is actually a shortcut for `int [][] a= new int[][] {{1,2,3}, {2,2,4}}` - this is only allowed when declaring an array, not in an assignment – user85421 Jun 18 '19 at 12:40

4 Answers4

5

Considering your basic example,

public class Matrix {

    int[][] _matrix;
}

You can initialize matrix as follows

public class Caller {

    public static void main(String[] args) {
        Matrix m = new Matrix();
        m._matrix = new int[][]{{1,2,3}, {2,2,4}};
    }
}
Chaitanya
  • 3,590
  • 14
  • 33
  • 1
    I didn't down-voted, but.... this initialization `int [][] a={{1,2,3}, {2,2,4}}` [is correct](https://ideone.com/6RmRij) too. – lealceldeiro Jun 18 '19 at 12:34
0
newMatrix._matrix = new int[][] {{1,2,3}, {2,2,4}};

The reason your code does not compile is because you are missing the new int[][] statement. It is required in a line, where you want to use a shortcut array initializer. See this question: How to initialize an array in Java?

TreffnonX
  • 2,924
  • 15
  • 23
  • 1
    Please explain how this fixes OP's problem. – Austin Schaefer Jun 18 '19 at 12:23
  • It fixes: "I try to write newMatrix._matrix={{1,2,3}, {2,2,4}} and i got a syntax error.", because that syntax error is founded in a missing new int-array statement. The compiler cannot resolve the double-brackets as array initializers. I was writing that in my answer, just now :) – TreffnonX Jun 18 '19 at 12:25
  • Then a simple "you're missing the `new int[][]` syntax" goes a long way in helping OP understand what they did wrong. And no problem, just wanted to make the answer more effective for them. – Austin Schaefer Jun 18 '19 at 12:26
  • I'm not mad at you; I just write [assertively](https://en.m.wikipedia.org/wiki/Assertiveness) when on this site. – Austin Schaefer Jun 18 '19 at 12:31
  • Ah, got it. I misunderstood the initial question 'Please explain how this fixes OP's problem.' as a bit offended. Maybe 'Please elaborate your answer'? :) – TreffnonX Jun 18 '19 at 12:36
  • @lealceldeiro `int [][] a={{1,2,3}, {2,2,4}};` is correct, but not `matrix._matrix = {{1,2,3}, {2,2,4}};`, because the compiler requires the `int [][]` to be inline. For example, `Object a = { 1, 2, 3 };` is not allowed, but both `Object a = new int[]{ 1, 2, 3 };` and `int[] a = { 1, 2, 3 };` are. – TreffnonX Jun 18 '19 at 12:39
0

newMatrix._matrix={{1,2,3}, {2,2,4}} is not allowed according to docs. I have added alternate way to achieve the same results.

package com.psl;

public class Test {

public static void main(String[] args) {

    Matrix matrix = new Matrix();

    matrix._matrix = new int[][]{{1,2,3}, {2,2,4}};


}

}
Akshay Bande
  • 2,491
  • 2
  • 12
  • 29
0

This way you can initialize your matrix

class Matrix {
    int[][] _matrix;

    public int [][] get_matrix(){
        return this._matrix;
    }

    public void set_matrix(int [][] a){
        this._matrix = a;
    }
}


public class Test {
    public static void main(String [] args){
        Matrix matrix = new Matrix();
        matrix.set_matrix(new int[][]{{1,2,3}, {2,2,4}});
        System.out.print(matrix.get_matrix());
    }
}

OR you can create a static properties in class and then initialize with class name

rohit prakash
  • 565
  • 6
  • 12