keySet和entrySet需要定义的方法?

use*_*542 -1 java treemap

这是相当荒谬的,我不确定为什么会发生这种情况,但每当我尝试使用.keySet或者.entrySet我在Eclipse中得到错误时都会为它创建一个新方法.

Sot*_*lis 6

该类型SparseMatrix未声明调用的方法entrySet.类型呢TreeMap.您应该在该类型的引用上调用该方法.可能实现一个将检索对象matrix字段的getter SparseMatrix.

// inside the SparseMatrix class
public TreeMap<Integer, TreeMap<Integer, Double> getMatrix() {
    return matrix;
}
Run Code Online (Sandbox Code Playgroud)

然后调用此方法并链接entrySet()调用

public static boolean equals(SparseMatrix a, SparseMatrix b) {
    System.out.println("The entry set is:\n" + a.getMatrix().entrySet());
}
Run Code Online (Sandbox Code Playgroud)

请记住,在编译时,方法调用是根据变量的类型来解决的.如果该类型未直接或通过继承声明该​​方法,则会发生编译错误.