简体   繁体   English

为什么我不能正确地从哈希表中检索?

[英]Why can't I properly retrieve from my hashtable?

Why can't I properly retrieve from my hashtable? 为什么我不能正确地从哈希表中检索? I create a hashtable which consists of key and values as both type Coordinate (a class I created). 我创建了一个由键和值组成的哈希表,二者均为Coordinate类型(我创建的一个类)。 And then I can't retrieve the x value from my coordinates object. 然后我无法从坐标对象中检索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;

}

Coordinates Class: 座标类别:

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;}

}

LOGTABLE: 记录表:

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

For a Hashtable (and HashMap ) to store and retrieve keys properly, the key type must override hashCode and equals properly. 为了使Hashtable (和HashMap )正确存储和检索键,键类型必须覆盖hashCode并正确地equals

To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. 为了成功地从哈希表存储和检索对象,用作键的对象必须实现hashCode方法和equals方法。

You haven't overridden those methods, so the Hashtable can't find your keys. 您尚未覆盖这些方法,因此Hashtable找不到您的键。 Override those methods. 覆盖那些方法。

You need to override the hashCode and equals method like so, so that it may be stored and retrieved from the HashTable/HashMap, etc.. 您需要像这样重写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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM