2014-01-07 47 views
0

我想建立和IP地址管理工具。 我想將使用的IP存儲在數據庫中,並在設備上使用IP地址 。我希望能夠將「已使用」的ips拔出,並將它們與來自給定子網的IP列表進行比較,並確定該IP是「已使用」還是「可用」。 我需要「可用」列表來生成一個列表,我可以選擇添加到新設備,而不必選擇「已用」ip。在Java中,如何比較兩個對象的值?

這是我試過的,但它似乎沒有比較IPAddress類型的兩個對象。

ArrayList<IPAddress> used = new ArrayList<>(); 
    ArrayList<IPAddress> available = new ArrayList<>(); 
    ArrayList<IPAddress> subnet = new ArrayList<>(); 

    //I reference my IPAddress class which has four int fields: 
    //oct_1, oct_2, oct_3, oct_4 
    //the constructor for IPAddress takes for Ints and I build my IPAddress 
    //from the for octets. 

    //build a list of all ips in the subnet 10.50.2.0 - 10.50.2.255 
    //assuming a class C subnet 255.255.255.0 
    for(int i = 0; i < 256; i++){ 
     subnet.add(new IPAddress(10,50,2,i)); 
    } 

    //identify used ips. Eventually want to pull these from a database 
    //but for now these are just random IP's representing possible used IPs 
    used.add(new IPAddress(10,50,2,3)); 
    used.add(new IPAddress(10,50,2,5)); 
    used.add(new IPAddress(10,50,2,9)); 
    used.add(new IPAddress(10,50,2,13)); 
    used.add(new IPAddress(10,50,2,17)); 
    used.add(new IPAddress(10,50,2,22)); 


    //**** NEEDED CODE ******** 
    //i want to iterate through each IP in the subnet and check if it's in 
    //the 'used' list. If it IS NOT add the ip to the 'available' list. 

    //my failed try  
    for(IPAddress ip : subnet){ 
     if(!used.contains(ip)){ //if ip is NOT in used add it to available 
      available.add(ip); 
     } 
    } 



    //print results out to the screen   
    for(IPAddress ip : available){ 
     System.out.println(ip.toString() + " is available."); 
    }  

    for(IPAddress ip: used){ 
     System.out.println(ip.toString() + " is used."); 
    } 




//******************** Here is my IPAddress Class if it helps **************** 
public class IPAddress { 
private int oct_1 = 0; 
private int oct_2 = 0; 
private int oct_3 = 0; 
private int oct_4 = 0; 

public IPAddress(int Oct_1, int Oct_2, int Oct_3, int Oct_4){ 
    oct_1 = Oct_1; 
    oct_2 = Oct_2; 
    oct_3 = Oct_3; 
    oct_4 = Oct_4; 
} 

@Override 
public String toString(){ 
    String ipAddress = ""; 

    if(getOct_1() != 0){ 
    ipAddress = getOct_1() + "." + getOct_2() + "." + getOct_3() + "." + getOct_4(); 
      } 
    return ipAddress; 

} 
+0

@RC .:這不會幫助解決問題。他想要一個簡單的包含與否,而不是排名或排序。 –

+0

如果你自己創建了這個IPAddress類型,我認爲你需要定義equals方法來告訴它如何進行比較。我只是用一個字符串來表示IP。請參閱http:// stackoverflow。com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java – developerwjk

回答

0

您可以編寫一個自定義比較器,該比較器匹配兩個對象的屬性並返回布爾值或覆蓋對象的equals方法。

@Override 
public boolean equals(Object other){ 
    if (other == null) return false; 
    if (other == this) return true; 
    if (!(other instanceof IPAddress))return false; 
    IPAddress otherIPAddress = (IPAddress)other; 
    //match other properties and return result 
    return otherIPAddress.oct_1 == this.oct_1 && otherIPAddress.oct_2 == this.oct_2 && otherIPAddress.oct_3 == this.oct_3 && otherIPAddress.oct_4 == this.oct_4; 
} 

我還想補充一點,你應該always override hashCode whenever overriding equals

+0

這似乎是最thourogh的答案,但我不知道如何重寫哈希碼。所以我選擇實施我自己的方法。 – MBFROMIT

5

contains方法將使用equals。確保您覆蓋IPAddress類中的equals方法。

此外,Javadocs for equals指出,只要equals被覆蓋,hashCode方法應該被覆蓋。

指示其他某個對象是否「等於」這一個。

注意,這是通常需要覆蓋每當這個方法被覆蓋hashCode方法,以便維持用於hashCode方法,其中指出,等於對象必須具有相等的總承包哈希碼。

+5

不要忘記還要注意hashCode方法也應該被覆蓋。 –

+0

@HovercraftFullOfEels我已經注意到了。謝謝。 – rgettman

1

您必須重寫equals方法(來自Object類)並定義兩個IPAddresses等於另一個的方式。另外,如果兩個對象相互相等,則它們必須具有相同的哈希碼,因此也要重寫哈希碼方法。

0

您需要實現類IPAdress的equals方法。理想情況下,您使用HashSet而不是列表,並在IPAddress類中實現hashCode方法以提高效率。

0

==運算符將通過查看兩個對象是否在內存中保存相同位置來確定相等性。

如果傳遞的對象與當前對象「相等」,則.equals函數返回true。重寫.equals以確定兩個對象是由您自己的邏輯相等的。如果您覆蓋.equals,則還應該覆蓋.hashcode函數。