In a first file I declared the Matrix class with corresponding generic variable and with a constructor defined as follows:
public class Matrix<T>{
private T values[][];
void Matrix(T values[][]){
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[0].length; j++) {
this.values[i][j] = values[i][j];
}
}
}
And in a second test file class with main program I declare the matrix of generic variables with String elements.
public abstract class TestMatrix {
public static void main(String[] args){
//Declare matrix
String[][] array = {{"a","b"},{"c","d"}};
//Initialize matrix
Matrix<String> m = new Matrix<String>(array);
}
Nevertheless I don´t quite understand why I get the error:
TestMatrix.java:6: error: constructor Matrix in class Matrix<T> cannot be applied to given types;
Matrix<String> m = new Matrix<String>(array);
If I´m using a 2d-dimensional array what´s wrong with the program? I have researched many sites but it´s actually hard to find the exact same example explained here and in the whole internet, therefore I find it quite suitable to post this question here to listen any answer I would gratefully appreciate.
Advertisement
Answer
You constuctor is not a constructor because of the void, like here So you just have the invisible constructor without arguments.
You don´t need to copy the arrays with the for loops you can just use build in stuff like Arrays.copyOf(array, newLength);