java java中的矩阵类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16381486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:49:16  来源:igfitidea点击:

A matrix class in java

java

提问by Itay4

I need to create a matrix class, which has 2 constructors, and a method which does a filter average to a number with the numbers around it in the matrix.

我需要创建一个矩阵类,它有 2 个构造函数,以及一个方法,该方法对一个数字进行过滤器平均,其中包含矩阵中的数字。

Here is what I came up with, but when I try to compile it, I get an error: "array expected, Matrix found".

这是我的想法,但是当我尝试编译它时,出现错误:“需要数组,找到矩阵”。

Help please !

请帮忙 !

/**
* 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];
}


}

Edit

编辑

Hey guys, thanks for the help. Now I'm trying to make a toString method, That will print the matrix, with tab between the numbers, but after the last number in the last row, there won't be a tab. Can't really get it working, don't know how to return it. But here is what I came up with so far:

嘿伙计们,感谢您的帮助。现在我正在尝试创建一个 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(); 
 } 


}

回答by nullptr

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];

This line causes an error because you can't use []on a Matrix. You can only use that operator on an array.

此行会导致错误,因为您不能[]Matrix. 您只能在数组上使用该运算符。

Also, this will cause an error because you haven't set _matrixequal to anything:

此外,这会导致错误,因为您没有设置_matrix等于任何内容:

for (int i=0; i < array.length;i++)
    for (int j=0; j < array[i].length;j++)
        _matrix[i][j] = array[i][j];

Add _matrix = new int[array.length][array[0].length]before those lines.

_matrix = new int[array.length][array[0].length]在这些行之前添加。

回答by Evgeniy Dorofeev

Change imageFilterAverage()

更改 imageFilterAverage()

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._matrix[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];
    return newMatrix;
}

compile error will go, but you probably have more bugs in the code, eg the second constructor must be like this

编译错误会去,但你可能在代码中有更多的错误,例如第二个构造函数必须是这样的

/**
 * Constructs a size1 by size2 Matrix of zeroes.
 */
public Matrix(int size1, int size2) {
    _matrix = new int[size1][size2];
}

The first constructor is also wrong because you first need to create _matrix then copy data.

第一个构造函数也是错误的,因为你首先需要创建 _matrix 然后复制数据。