In an attempt to see if I can clean up some of my math code, mostly matrix stuff, I am trying to use some Java Generics. I have the following method:
private <T> T[][] zeroMatrix(int row, int col) {
T[][] retVal = (T[][])new Object[row][col];
for(int i = row; i < row; i++) {
for(int j = col; j < col; j++) {
retVal[i][j] = 0;
}
}
return retVal;
}
The line retVal[i][j] = 0 is the one causing me headaches. The goal of the line is to initialize the array with the T representation of 0. I've attempted to do all sorts of things with it: (T is defined in the class as T extends Number)
retVal[i][j] = (T)0;
retVal[i][j] = new T(0);
The only thing that works is
retVal[i][j] = (T)new Object(0);
Which is not what I want.
Is this possible? Is there an easier way to represent an NxM matrix of any type of Number(including potentially BigDecimal), or am I stuck?