我正在尝试使用哈希映射实现矩阵,但我认为我在某处做错了。 如果我输入以下矩阵:
4 3
2 1
我得到了
0 3
0 0
这是我的代码:
package matrix;
import java.util.HashMap;
public class SparseMatrix extends AbstractMatrix{
private int x=0; // nr of rows
private int y=0; // nr of columns
private int hashvalue = 0;
public void Index (final int x, final int y)
{
this.x=x;
this.y=y;
hashvalue=((x+"")+(y+"")).hashCode();
}
public boolean equals (final Object obj)
{
if (obj instanceof Index)
{
Index index = (Index) obj;
return ((x==index.x) && (y==index.y));
}
else
return false;
}
public int hashCode()
{
return hashvalue;
}
private int rows;
private int columns;
private HashMap<Index,Double> values;
private boolean validIndex (final int row, final int column)
{
return (row>=0 && row<rows && column>=0 && column<columns);
}
public SparseMatrix(double[][] contents) throws MatrixException {
this(contents.length,contents[0].length);
for (int i=0; i<contents.length; i++)
for (int j=0; j<contents[0].length; j++)
setElement(i,j,contents[i][j]);
}
public SparseMatrix(int rows, int columns) throws MatrixException {
if (rows<1 || columns<1)
throw new MatrixException("Number of rows and columns cannot be less than 1");
this.rows=rows;
this.columns=columns;
this.values=new HashMap<Index,Double>();
}
@Override
public int getNumberOfRows() {
return rows;
}
@Override
public int getNumberOfColumns() {
return columns;
}
@Override
public double getElement(int row, int column) throws MatrixException {
if (!validIndex(row,column))
throw new MatrixException (row,column);
Index index = new Index (row,column);
if (values.containsKey(index))
return values.get(index);
else
return 0;
}
@Override
public void setElement(int row, int column, double value) throws MatrixException {
if (!validIndex(row,column))
throw new MatrixException (row,column);
Index index = new Index(row,column);
if (value==0)
{
if (values.containsKey(index))
values.remove(index);
}
else
values.put(index,value);
}
}
和我的主要功能:
a2 = new SparseMatrix(2,2);
a1.setElement(0,0,4);
a1.setElement(0,1,3);
a1.setElement(1,0,2);
a1.setElement(1,1,1);
for (int i=0; i<a2.getNumberOfRows(); i++)
{
for (int j=0; j<a2.getNumberOfColumns(); j++)
System.out.print (a2.getElement(i, j)+ " ");
System.out.println();
}
任何想法我做错了什么?
答案 0 :(得分:1)
public void Index (final int x, final int y)
看起来这应该是Index类的构造函数,但它是一种方法,我不会在任何地方看到Index类。
您正在为SparseMatrix
分配a2
的实例,但为setElement
致电a1
:
a2 = new SparseMatrix(2,2);
a1.setElement(0,0,4);
a1.setElement(0,1,3);
a1.setElement(1,0,2);
a1.setElement(1,1,1);
我尝试了您的代码,修复了所有编译错误,而不依赖于AbstractMatrix
超类(通过从所有重写方法中删除@Override
并更改类顶部的代码到一个名为Index的嵌套类中,得到了所需的输出:
4.0 3.0
2.0 1.0
这是我使用的嵌套Index
类:
static class Index {
private int x=0; // nr of rows
private int y=0; // nr of columns
private int hashvalue = 0;
public Index (final int x, final int y)
{
this.x=x;
this.y=y;
hashvalue=((x+"")+(y+"")).hashCode();
}
public boolean equals (final Object obj)
{
if (obj instanceof Index)
{
Index index = (Index) obj;
return ((x==index.x) && (y==index.y));
}
else
return false;
}
public int hashCode()
{
return hashvalue;
}
}
答案 1 :(得分:0)
我认为你的构造函数应该引用contents [i]而不是contents [0]
public SparseMatrix(double[][] contents) throws MatrixException {
this(contents.length,contents[0].length);
for (int i=0; i<contents.length; i++)
for (int j=0; j<contents[i].length; j++)
setElement(i,j,contents[i][j]);
}