我需要创建一个矩阵类,它有 2 个构造函数,以及一个对矩阵中的数字进行过滤平均的方法。
这是我想出的,但是当我尝试编译它时,我得到一个错误:“array expected, Matrix found”。
请帮忙 !
/**
* This class represents a two dimensional Matrix
*
*/
public class Matrix
{
private int[][] _matrix;
/**
* Constructs a Matrix from a two-dimensional array; the dimensions as well as the values of this Matrix will be the same as the dimensions and values of the two-dimensional array.
*/
public Matrix(int[][] array)
{
for (int i=0; i < array.length;i++)
for (int j=0; j < array[i].length;j++)
_matrix[i][j] = array[i][j];
}
/**
* Constructs a size1 by size2 Matrix of zeroes.
*/
public Matrix(int size1, int size2)
{
for (int i=0; i < size1;i++)
for (int j=0; j < size2;j++)
_matrix[i][j]=0;
}
/**
* Calculates and returns a copy of this Matrix after it has been filtered from noise. All pixels are represented by a number 0-255 inclusive.
*
* @return a copy of this Matrix after it has been filtered from noise
*/
public Matrix imageFilterAverage()
{
Matrix newMatrix = new Matrix(_matrix);
for (int i=0; i < _matrix.length;i++)
for (int j=0; i < _matrix[i].length;j++)
newMatrix[i][j] = _matrix[i-1][j-1] + _matrix[i-1][j] + _matrix[i-1][j+1] + _matrix[i][j-1] + _matrix[i][j] + _matrix[i][j+1] + _matrix[i+1][j-1] + _matrix[i+1][j] + _matrix[i+1][j+1];
}
}
编辑
嘿伙计们,谢谢你的帮助。现在我正在尝试制作一个 toString 方法,它将打印矩阵,数字之间有制表符,但在最后一行的最后一个数字之后,将没有制表符。实在不行,不知道怎么退货。但这是我到目前为止想出的:
public String toString()
{
for (int i=0; i < _matrix.length; i++) {
for (int j=0; j < _matrix[i].length; j++)
if (j == (_matrix[i].length - 1))
System.out.print(_matrix[i][j]);
else System.out.print(_matrix[i][j] + "\t");
System.out.println();
}
}