我需要建立一個職位經理職位來告訴我職位是否可用。我需要什麼樣的數據結構來實現存儲座標的哈希列表?
所以我想這:
enter code here
public class PositionManager {
Hashtable currentPositions = new Hashtable();
void occupiedPosition(int x,int y){
this.currentPositions.put(new Integer("4"),new Integer("5"));
this.currentPositions.put(new Integer("1"),new Integer("5"));
this.currentPositions.put(new Integer("11"),new Integer("3"));
this.currentPositions.put(new Integer("42"),new Integer("55"));
this.currentPositions.put(new Integer("11"),new Integer("53"));
Set keys = this.currentPositions.keySet(); // The set of keys in the map.
Iterator keyIter = keys.iterator();
System.out.println("The map contains the following associations:");
while (keyIter.hasNext()) {
Object key = keyIter.next(); // Get the next key.
Object value = this.currentPositions.get(key); // Get the value for that key.
System.out.println(" (" + key + "," + value + ")");
}
}
public static void main(String[] args) {
new PositionManager().occupiedPosition(3, 3);
}
}
當然這只是一個測試,我所試圖做的是retreiving所使用的所有位置,問題是,我不能有重複的密鑰。 那麼我應該使用什麼樣的數據結構。 在此先感謝。
當你試圖插入一個重複密鑰完成時,你希望結構行爲如何?你希望它完全忽略新的鍵/值對嗎?或用新的價值取代舊價值? – dharga 2009-12-01 21:22:08