【问题标题】:Sparse Matrices in JavaJava中的稀疏矩阵
【发布时间】:2018-08-12 19:00:13
【问题描述】:

稀疏矩阵是元素主要为零的矩阵。下面的代码使用 LinkedLists 的 ArrayList 来实现稀疏矩阵。它定义了一个类 Element 来存储元素的列号和值。每行仅由具有非零值的元素的 LinkedList 表示。很少(如果有的话)行全为零,因此 ArrayList 用于按行升序存储每一行​​的 LinkedList。

class Element { 
  public int column; 
  public int value; }

public class SparseMatrix { 
  private int mRows; // Number of rows 
  private int mCols; // Number of columns 
  private ArrayList<LinkedList<Element>> mMatrix;

1) 如何使用带有参数 (int r, int c) 的 getter,它提供特定的行号和列号来检索矩阵的该行和列中的值?

2) 如何使用带参数 (int r, int c, int v) 的 setter 将 r 行和 c 列的值设置为 v? (注意:如果节点不存在,则必须创建一个新节点。如果 v 为零,则删除该节点。)

如果我错了,请纠正我,但是要分别获得矩阵的总行数和总列数,我会这样做:

get(int r, int c) {
    int rowSize = mMatrix.length;
    int colSize = mMatrix[0].length;
    }

但是,我不确定以后如何使用它。

【问题讨论】:

  • Q1:如何检查 mMatrix 是否有足够的行用于 'r'?如果没有,你如何“成长”它? Q2:您如何获得给定行的特定 LinkedList? Q3:如果有一行的链表,你将如何扫描以检查列c是否已经存在一个元素?如果你找到一个,你将如何获取/设置它的值?如果你不这样做,你将如何制作一个新的并将其添加到列表中? ...发布您为这些步骤中的至少几个所做的尝试应该会提高人们帮助您的愿望。

标签: java matrix getter-setter


【解决方案1】:

我将在此处使用一些基本的 cmets 删除代码。您应该可以根据自己的需要进行调整。 我不会使用class Element,因为它持有一个int。 column 的价值意义无关紧要。

private static int mRows; // Number of rows 
private static int mCols; // Number of columns 
private static final ArrayList<LinkedList<Integer>> mMatrix = new ArrayList<>();


public static void main(String[] args) {
    mRows = 7; //example
    mCols = 4; //example

     //init your matrix
    for (int i = 0; i < mRows; i++) { //add new row 7 times
        mMatrix.add(new LinkedList<>());
        for (int j = 0; j < mCols; j++) {
            mMatrix.get(i).add(0); // add Integer with value 0 (4 times)
        }
    }

    //test
    setValue(5, 3, 159);
    System.out.println(getValue(5, 3));

}

public static void setValue(int r, int c, Integer v) {
    //before call be sure that r < mRows and c < mCols
    mMatrix.get(r).set(c, v); //replaces existing Integer
}

public static Integer getValue(int r, int c) {
    //before call be sure that r < mRows and c < mCols
    return mMatrix.get(r).get(c);
}

【讨论】:

  • 非常感谢!
【解决方案2】:

我可能不会使用列表,我会在幕后使用Map&lt;Point,T&gt;

未经测试的代码:

interface Matrix<T> {
    public T get(Point p);
    public void put(Point p, T v);
}

class SparseMatrix<T extends Number> implements Matrix<T> {
    Map<Point,T> map = new HashMap<>();
    @Override
    public T get(Point p) {
        return map.get(p);
    }

    @Override
    public void put(Point p, T v) {
        if ( v.doubleValue() == 0.0) {
            // Any zeros get removed.
            map.remove(p);
        } else {
            map.put(p, v);
        }
    }
}

class Point {
    final int row;
    final int col;

    Point(int row, int col) {
        this.row = row;
        this.col = col;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Point point = (Point) o;

        if (row != point.row) return false;
        return col == point.col;

    }

    @Override
    public int hashCode() {
        int result = row;
        result = 31 * result + col;
        return result;
    }
}

【讨论】:

  • 聪明,但记住 HashMap 的内存开销,
猜你喜欢
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 2010-09-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-10
  • 2015-04-26
相关资源
最近更新 更多