Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将每个枚举值作为字符串列表获取?_Java_Enums - Fatal编程技术网

Java 如何将每个枚举值作为字符串列表获取?

Java 如何将每个枚举值作为字符串列表获取?,java,enums,Java,Enums,如何获取此枚举的所有字符串? 我想要像这样的东西: 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"), U

如何获取此枚举的所有字符串? 我想要像这样的东西:

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")
List defaultAddresses=new ArrayList();
对于(CountryDefaultAddressEnum countryAddr:CountryDefaultAddressEnum.values())
{
defaultAddresses.add(countryAddr.toString());
}

如果我理解您的问题,您将需要一个返回带有枚举值的字符串的方法

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

}
结果:


这不是有效的枚举。你的构造器在哪里?您可以发布它吗?添加一个方法
List getValue
,该方法返回正确的列表,其中包含
enum
中的所有值。for(CountryDefaultAddressEnum countryAddr:CountryDefaultAddressEnum.US_CAL.values()){System.out.println(countryAddr+“:“+countryAddr.getValue());}@RuchiSethi然后你会使用US_-CAL.getValue()for(CountryDefaultAddressEnum countryAddr:US_-CAL.values())也会同时为constantsUS_-CAL.getValue()工作..:)我在for循环中使用了它,不是吗have@RuchiSethi很高兴听到:-)检查我的答案,这样你的问题就解决了。我已经编辑了代码,现在应该可以编译了(我的编辑仍需要批准,因此可能现在不显示)
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);
    }
}