2015-07-10 122 views
0

我正在使用HashMap。我的代碼會編譯,但是當我運行它時,我沒有得到我應該期待的。我的代碼爲printAllNeighborsgetNeighbor不起作用。誰能幫我。我想我明白我在做什麼,這就是爲什麼我很困惑。HashMap問題,打印鄰居

import java.util.HashMap; 
public class House 
{ 
    private int streetNumber; 
    private String streetName; 
    private String owners; 
    HashMap <String, House> myNeighbors; 

    public House (int streetNumber, String streetName, String lastName) 
    { 
     streetNumber = streetNumber; 
     streetName = streetName; 
     lastName = owners; 
     myNeighbors = new HashMap <String, House>(); 
    } 
    public int getStreetNumber() 
    { 
     return streetNumber; 
    } 
    public String getStreetName() 
    { 
     return streetName; 
    } 
    public String getLastName() 
    { 
     return owners; 
    } 
    public void setStreetNumber(int streetNumber) 
    { 
     streetNumber =this.streetNumber; 
    } 
    public void setStreetName(String streetName) 
    { 
     streetName = this.streetName; 
    } 
    public void setLastName(String lastName) 
    { 
     owners = lastName; 
    } 
    public void addNeighbor (String direction, House h) 
    { 
     myNeighbors = new HashMap <String, House>(); 
    } 
    public House getNeighbor (String direction) 
    { 
     if(direction.equals(direction)){ 
      return getNeighbor(direction); 
     } 
     else{    
      return null; 
     } 
    } 
    public void printAllNeighbors() 
    { 
     for (String key : myNeighbors.keySet()) 
     { 
      System.out.println("\nkey: " + key + " value: " + myNeighbors.get(key)); 
     } 
    } 
    public String toString() 
    { 
     return "Address: " + streetNumber + " " + streetName + "\nOwner: " + owners; 
    } 
    public static void main (String [] args) 
    { 
     House whiteHouse = new House (1600, "Pennsylvania Ave", "Obama"); 
     House jerrysHouse = new House (1602, "Pennsylvania Ave", "Mabrito"); 
     House guentersHouse = new House (1604, "Pennsylvania Ave", "Tusch"); 
     House anasHouse = new House (1609, "Pennsylvania Ave", "Posada"); 

     whiteHouse.addNeighbor("east", jerrysHouse); 
     whiteHouse.addNeighbor("south", guentersHouse); 
     whiteHouse.addNeighbor("north west", anasHouse); 

     System.out.println(whiteHouse);  
     whiteHouse.printAllNeighbors(); 

     System.out.println("****************************** \n" + jerrysHouse); 
     jerrysHouse.printAllNeighbors(); 

     System.out.println("****************************** \n" + guentersHouse); 
     guentersHouse.printAllNeighbors(); 

     System.out.println("****************************** \n" + anasHouse); 
     anasHouse.printAllNeighbors(); 
    } 
} 
+0

我想你需要告訴我們你得到了什麼,以及你的期望。 – ThisClark

+0

我得到空0,業主0,我期待得到白宮,jerrysHouse,guentersHouse等的鄰居。 –

+0

嗨@Deborah_Watson如果任何答案已解決您的問題,請點擊複選標記考慮[接受它](http://meta.stackexchange.com/q/5234/179419)。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – gastonmancini

回答

2

問題是與你的addNeighbor方法,您要創建一個新的HashMap對象每次調用它,而不是將元素添加到地圖的時間。

嘗試使用HashMap put方法。例如:

myNeighbors.put(direction,h); 
0

你想做什麼?

public void addNeighbor (String direction, House h) 
    { 
     //myNeighbors = new HashMap <String, House>(); This is not correct 
     myNeighbors.put(direction,h); // use this 
    } 

並初始化你映射在像下面開始時,

Map <String, House> myNeighbors= new HashMap<String, House>(); 
0

,我發現你的代碼存在以下問題。首先在構造函數 中如果你想引用對象字段,你應該使用這個引用,否則它只會更新局部變量。

public House (int streetNumber, String streetName, String lastName) 
{ 
this.streetNumber = streetNumber; 
this.streetName = streetName; 
this.owners = lastName // Note this you were previously assigning value of owner to lastName 
myNeighbors = new HashMap <String, House>(); 
} 

,並使用

myNeighbors.put(direction,h); 

代替

myNeighbors = new HashMap <String, House>(); 
0

問題是與你的構造和setter方法。你正在做沒有任何作用的作業。例如,

public House (int streetNumber, String streetName, String lastName) 
{ 
    streetNumber = streetNumber; 
    streetName = streetName; 
    lastName = owners; 
    myNeighbors = new HashMap <String, House>(); 
} 

從上面的構造函數,你還沒有正確地初始化字段。 'streetNumber'和'streetNames'是你的構造函數中的參數,所以在影子上隱藏(隱藏)私有數據字段'streetNumber'和'streetName',要訪問這個私有數據字段,你應該使用'this'關鍵字。而不是 lastName =擁有者; 在你的構造函數中,它應該是 owners = lastName; 因此,這是修改後的構造

public House (int streetNumber, String streetName, String lastName) 
{ 
    this.streetNumber = streetNumber; 
    this.streetName = streetName; 
    owners = lastName; 
    myNeighbors = new HashMap <String, House>(); 
} 

你的setter方法有同樣的問題,所以對於setStreetNumber()方法, 變化

streetNumber = streetNumber; to 
this.streetNumber = streetNumber; 

和setStreetName做同樣的()方法

0

我已經對你的代碼做了一些修改,以使它在運行時回覆,如果有些東西不符合你的期望

import java.util.HashMap; 
public class House 
{ 
    private int streetNumber; 
    private String streetName; 
    private String owners; 
    HashMap <String, House> myNeighbors; 

    public House (int streetNumber, String streetName, String lastName) 
    { 
     this.streetNumber = streetNumber; 
     this.streetName = streetName; 
     this.owners = lastName; 
     this.myNeighbors = new HashMap <String, House>(); 
    } 
    public int getStreetNumber() 
    { 
     return streetNumber; 
    } 
    public String getStreetName() 
    { 
     return streetName; 
    } 
    public String getLastName() 
    { 
     return owners; 
    } 
    public void setStreetNumber(int streetNumber) 
    { 
     streetNumber =this.streetNumber; 
    } 
    public void setStreetName(String streetName) 
    { 
     streetName = this.streetName; 
    } 
    public void setLastName(String lastName) 
    { 
     owners = lastName; 
    } 
    public void addNeighbor (String direction, House h) 
    { 
     myNeighbors.put(direction,h); 
    } 
    public House getNeighbor (String direction) 
    { 
     return myNeighbors.get(direction); 
    } 
    public void printAllNeighbors() 
    { 
     for (String key : myNeighbors.keySet()) 
     { 
      System.out.println("\nkey: " + key + " value: " + myNeighbors.get(key)); 
     } 
    } 
    public String toString() 
    { 
     return "Address: " + streetNumber + " " + streetName + "\nOwner: " + owners; 
    } 
    public static void main (String [] args) 
    { 
     House whiteHouse = new House (1600, "Pennsylvania Ave", "Obama"); 
     House jerrysHouse = new House (1602, "Pennsylvania Ave", "Mabrito"); 
     House guentersHouse = new House (1604, "Pennsylvania Ave", "Tusch"); 
     House anasHouse = new House (1609, "Pennsylvania Ave", "Posada"); 

     whiteHouse.addNeighbor("east", jerrysHouse); 
     whiteHouse.addNeighbor("south", guentersHouse); 
     whiteHouse.addNeighbor("north west", anasHouse); 

     System.out.println(whiteHouse);  
     whiteHouse.printAllNeighbors(); 

     System.out.println("****************************** \n" + jerrysHouse); 
     jerrysHouse.printAllNeighbors(); 

     System.out.println("****************************** \n" + guentersHouse); 
     guentersHouse.printAllNeighbors(); 

     System.out.println("****************************** \n" + anasHouse); 
     anasHouse.printAllNeighbors(); 
    } 
}