【问题标题】:Why can't I properly retrieve from my hashtable?为什么我不能从我的哈希表中正确检索?
【发布时间】:2015-03-17 18:07:20
【问题描述】:

为什么我不能从我的哈希表中正确检索?我创建了一个哈希表,其中包含键和值作为坐标类型(我创建的一个类)。然后我无法从我的坐标对象中检索 x 值。

public coordinates translateOffsetToPixel(int x, int y){
    //Translate given coordinates to pixel coordinates for the cell
    coordinates input = new coordinates(x,y);
    coordinates outputPixelCoord;

    Hashtable <coordinates, coordinates> table = new Hashtable<coordinates, coordinates>();

    for (int r = 0 ; r<row; r++){
        for(int c = 0; c< col; c++){
            System.out.println("hit translation");
            table.put(new coordinates(r,c), new coordinates(r*2,c*2));
        }
    }

    outputPixelCoord = table.get(input);
    System.out.println("outputX:" + outputPixelCoord.getX()); //ERROR
    return outputPixelCoord;

}

坐标类:

public class coordinates {
    private int x,y;
    public coordinates(int x, int y){
        this.x = x;
        this.y = y;
    }

    public int getX() {return x;}

    public int getY() {return y;}

}

记录表:

03-17 13:55:53.690    1961-1961/com.example.sam.matrix D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
03-17 13:55:53.780    1961-1961/com.example.sam.matrix I/System.out﹕ hit board
03-17 13:55:53.780    1961-1961/com.example.sam.matrix I/System.out﹕ 5
03-17 13:55:53.780    1961-1961/com.example.sam.matrix I/System.out﹕ hit translation
03-17 13:55:53.780    1961-1961/com.example.sam.matrix E/InputEventReceiver﹕ Exception dispatching input event.
03-17 13:55:53.780    1961-1961/com.example.sam.matrix E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
03-17 13:55:53.800    1961-1961/com.example.sam.matrix E/MessageQueue-JNI﹕ java.lang.NullPointerException

【问题讨论】:

  • java.lang.NullPointerException
  • 您永远不会在 table 映射中添加一个条目,该映射以 input 作为键,这就是 table.get(input) 将返回 null 的原因。
  • 这是输入表格的错误语句吗? table.put(新坐标(r,c), 新坐标(r*2,c*2));

标签: java hash logic hashtable


【解决方案1】:

要让Hashtable(和HashMap)正确存储和检索密钥,密钥类型必须正确覆盖hashCodeequals

要成功地从哈希表中存储和检索对象,用作键的对象必须实现 hashCode 方法和 equals 方法。

您尚未覆盖这些方法,因此Hashtable 无法找到您的密钥。覆盖这些方法。

【讨论】:

    【解决方案2】:

    您需要像这样重写 hashCode 和 equals 方法,以便可以从 HashTable/HashMap 等中存储和检索它。

    public class Coordinates {
        private int x,y;
        public coordinates(int x, int y){
            this.x = x;
            this.y = y;
        }
    
        public int getX() {return x;}
    
        public int getY() {return y;}
    
        public int hashCode() {
            // This is just a random way to generate hash
            // see other ways to generate hash before you implement this
            return x + (37 * y)
        }
    
        public boolean equals(Object obj) {
            if (obj instance of Coordinates) {
                Coordinates c = (Coordinates)obj;
                return c.x == this.x && c.y == this.y;
            }
            return false;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-11
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多