我有以下代码:
public class Matrix<K, V> {
private final List<K> colKeys;
private final List<K> rowKeys;
private final Map<K, Map<K, V>> matrix;
public Matrix() {
colKeys = new LinkedList<>();
rowKeys = new LinkedList<>();
matrix = new TreeMap<>();
}
...
public V getCell(K row, K col) {
return matrix.get(row).get(col);
}
...
}
Run Code Online (Sandbox Code Playgroud)
我像这样在另一个类中访问它:
Matrix matrix = new Matrix<String, Double>();
...
for (String colKey : colKeys) {
double sum = 0;
int divider = 0;
for (String rowKey : rowKeys) {
if (matrix.containsCell(rowKey, colKey)) …Run Code Online (Sandbox Code Playgroud)