2015-05-08 25 views
0
public enum CountryDefaultAddressEnum { 
    // Country or Region , Contact Name, Street Address, City, State, ZIP Code 
    US_CAL("US", "CA Address", "2065 Hamilton Avenue", " ", "San Jose", "CA", 
      "95125"), 
    US_SJ("US", "CA Address", "201 South Fourth Street", " ", "San Jose", "CA", 
      "95112"); 

    private CountryDefaultAddressEnum(String countryOrRegion, 
      String contactName, String streetAddress, String streetAddress2, 
      String city, String state, String zipCode) { 
     this.countryOrRegion = countryOrRegion; 
     this.contactName = contactName; 
     this.streetAddress = streetAddress; 
     this.streetAddress2 = streetAddress2; 
     this.city = city; 
     this.state = state; 
     this.zipCode = zipCode; 
    } 

public String getValue() { 
    return String.format("%s %s %s %s %s %s %s", countryOrRegion, contactName, streetAddress, streetAddress2, city, state, zipCode); 
    } 
} 

public static void main(String args[]) { 
    for(CountryDefaultAddressEnum countryAddr : US_CAL.values()) { 
     System.out.println(countryAddr + ": " + countryAddr.getValue()); 
    } 
} 

如何獲取此枚舉的所有字符串? 我想是這樣的:如何獲取每個枚舉值作爲一個字符串列表?

CountryDefaultAddressEnum.US_CAL.getValue()=("US", "CA Address", "2065 Hamilton Avenue", 
              " ", "San Jose", "CA", "95125") 
+3

這不是一個有效的枚舉。你的構造函數在哪裏?你可以發佈它嗎? – pathfinderelite

+0

添加一個'List getValue'的方法,該方法返回包含'enum'中所有值的正確列表。 –

回答

0

如果我理解你的問題,你會想返回你的枚舉值的字符串的方法。

public static void main(String args[]) { 
    for(CountryDefaultAddressEnum countryAddr : CountryDefaultAddressEnum.values()) { 
     System.out.println(countryAddr + ": " + countryAddr.getValue()); 
    } 
} 

public enum CountryDefaultAddressEnum { 

    // Country or Region , Contact Name, Street Address, City, State, ZIP Code 
    US_CAL("US", "CA Address", "2065 Hamilton Avenue", " ", "San Jose", "CA", 
      "95125"), 
    US_SJ("US", "CA Address", "201 South Fourth Street", " ", "San Jose", "CA", 
      "95112"); 

    private String countryOrRegion; 
    private String contactName; 
    private String streetAddress; 
    private String streetAddress2; 
    private String city; 
    private String state; 
    private String zipCode; 

    private CountryDefaultAddressEnum(String countryOrRegion, 
      String contactName, String streetAddress, String streetAddress2, 
      String city, String state, String zipCode) { 
     this.countryOrRegion = countryOrRegion; 
     this.contactName = contactName; 
     this.streetAddress = streetAddress; 
     this.streetAddress2 = streetAddress2; 
     this.city = city; 
     this.state = state; 
     this.zipCode = zipCode; 
    } 

    public String getValue() { 
     return String.format("%s %s %s %s %s %s %s", countryOrRegion, contactName, streetAddress, streetAddress2, city, state, zipCode); 
    } 
} 

結果:

enter image description here

+0

for(CountryDefaultAddressEnum countryAddr:CountryDefaultAddressEnum.US_CAL.values()){ System.out.println (countryAddr +「:」+ countryAddr.getValue()); } –

+0

@RuchiSethi然後你會使用US_CAL.getValue() – Shar1er80

+0

for(CountryDefaultAddressEnum countryAddr:US_CAL.values())也獲得兩個常量的值 –

0

你可以嘗試這樣的事情:

List<String> defaultAddresses = new ArrayList<String>(); 
for(CountryDefaultAddressEnum countryAddr : CountryDefaultAddressEnum.values()) 
{ 
    defaultAddresses.add(countryAddr.toString()); 

} 
+0

我編輯了代碼,它現在應該編譯(我的編輯仍然需要批准,所以它現在可能不會顯示) – Iamsomeone

相關問題