Quote:
Originally Posted by servlet
Can you paste line number from exception stacktrace
|
I don't think that will be necessary:
PHP Code:
public Matrix(int r,int c)
{
numRows = r;
numCols = c;
}
You're array list does not exist in your second example. Attempting to call list.get will result in a nullpointerexception on your m2.displayMatrix call when you attempt to run getValue on a non-object list. Consider chaining your constructors to prevent this from happening.
That matrix should be cast to a generic type as well and applied to your data list. With a matrix, I wouldn't use an array list at all; the scalar 2 dimensional array makes more sense for a matrix (unless you're talking about hundreds of thousands of sparse entries, where some trickery will need to be performed which usually involves a list of some sorts; does not apply to dense entries for which a matrix usually is).
PHP Code:
class Matrix<T>
{
private T[][] list;
private int iRows;
private int iCols;
public Matrix()
{
this(0, 0);
}
public Matrix(int iRows, int iCols)
{
this(iRows, iCols, null);
}
public Matrix(int iRows, int iCols, T data)
{
this.iRows = iRows;
this.iCols = iCols;
this.list = (T[][])new Object[iRows][iCols];
if (data != null)
{
for (int i = 0; i < iRows; ++i)
{
for (int j = 0; j < iCols; ++j)
{
this.list[i][j] = data;
}
}
}
}
}
Then change the method signatures to match T instead and rewrite for the (much easier) array instead of the collection.
This of course only works if you are either happy with only accepting objects and not primitives. If you wanted to do primitives you'd need to write an setValue overload for each type and wrap them into their corresponding datatype.