string - Take an array of values and compare them to Enum values in java -
string - Take an array of values and compare them to Enum values in java -
so trying in java take in set of values , compare values against same category of values in enum can find specific enum.
in other words have bunch of enums
public enum fruits{ apple("red", "round", "fruit", true), //about 20 or more constants orange("orange","sphere","citrus", true); private string color; private string shape; private string category; private boolean edible; private fruits(string color, string shape, string category, boolean edible){ this.color = color; this.shape = shape; this.category = category; this.edible = edible; } //getter methods each variable }
now have array of strings
string[] mystringarray = {"blue", "oblong", "berry"}; string[] mystringarraytwo = {"red", "round", "fruit"};
how compare string arrays string values in enums can figure out enum constant string array represents.
in reality have class reads in these string values file array , want assign enum class can give predefined classification can used other classes.
(note: constructor needs not private
-- default enums)
you code method in enum such as:
public static findbycharacteristics(final string[] characteristics) { (final fruits candidate: values()) if (candidate.color.equals(characteristics[0]) && candidate.shape.equals(characteristics[1]) && candidate.category.equals(characteristics[2])) homecoming candidate: homecoming null; }
then would:
final fruits f = fruits.findbycharacteristics(thearray); // check whether f null; if not, have match
however, imho, code "smells"; should seek different approach. 1 such "smell" method above requires array @ to the lowest degree 3 elements.
java string enums
Comments
Post a Comment