views:

33

answers:

3

I've started writing a class to model a matrix and the compiler gives me this message:

Matrix.java:4: cannot find symbol
symbol  : constructor Matrix(int[][])
location: class Matrix
  Matrix y = new Matrix(x);

This is the code that I was trying to compile:

public class Matrix<E> {
    public static void main(String[] args) {
        int[][] x = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}, {1, 2, 3}};
        Matrix y = new Matrix(x);
        System.out.println(y.getRows());
        System.out.println(y.getColumns());
    } 
    private E[][] matrix;
    public Matrix(E[][] matrix) {this.matrix = matrix;}
    public E[][] getMatrix() {return matrix;}
    public int getRows(){return matrix.length;}
    public int getColumns(){return matrix[0].length;}
}

So, my question is, why am I getting this error, and what should I change to fix this?

A: 

There is no generic array creation in Java. See http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation

Sasi
This doesn't really pertain to what I'm doing with this class.
Rafe Kettler
+1  A: 

Try using Integer[][] instead of int[][]. Your constructor expects the former (since there are no primitive type arguments), and you are passing the latter.

Bozho
Why is this upvoted? The real problem is the generic part of the type declaration is missing. This answer misses the mark.
Erick Robertson
+2  A: 

Try it like this:

public class Matrix<E> {
    public static void main(String[] args) {
        Integer [][] x = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {0, 1, 0}};
        Matrix<Integer> y = new Matrix<Integer>(x);
        System.out.println(y.getRows());
        System.out.println(y.getColumns());

        System.out.println("before: " + y);
        Integer [][] values = y.getMatrix();
        values[0][0] = 10000;
        System.out.println("after : " + y);
    }
    private E[][] matrix;
    public Matrix(E[][] matrix) {this.matrix = matrix;}
    public E[][] getMatrix() {return matrix;}
    public int getRows(){return matrix.length;}
    public int getColumns(){return matrix[0].length;}
    public String toString()
    {
        StringBuilder builder = new StringBuilder(1024);
        String newline = System.getProperty("line.separator");

        builder.append('[');
        for (E [] row : matrix)
        {
            builder.append('{');
            for (E value : row)
            {
                builder.append(value).append(' ');
            }
            builder.append('}').append(newline);
        }
        builder.append(']');

        return builder.toString();
    }
}

Compiles and runs on my machine.

You need to think about something else: encapsulation and when "private" isn't private. Check out the modification to the code and see how I'm able to modify your "private" matrix.

duffymo
Thanks, compiles and runs on my machine too. Guess I should have read up a bit more on syntax for generics before I got started with them
Rafe Kettler
Why not accept the answer?
duffymo
SO has a time limit for how quickly you can accept an answer. I tried a little ago, but I'll have to wait a little while to accept (unless someone else comes up with a super amazing answer that proves the existence of the god particle or something crazy like that)
Rafe Kettler