문제

I have this Java enum:

public enum ConnectionParameter {
    state,
    host,
    port,
    secure,
    username,
    password
}

that contains the fields of an XML file on which I have to work using XPATH (but this is not important at this time)

I have to iterate on all this enum fields to do an operation for each one.

How can I do it?

Tnx

Andrea

도움이 되었습니까?

해결책

for(ConnectionParameter item : ConnectionParameter.values())

다른 팁

You can use ConnectionParameter.values() that returns you an array with all the constants defined in the enum. Example:

for (ConnectionParameter c :ConnectionParameter.values()) {
   System.out.println(c.printableName());
}

More info:

You can use the values() method like this:

for (ConnectionParameter connectionParameter : ConnectionParameter.values()) {
   // do somethign with `connectionParameter` here
}

Every enum has this values() method.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top