Naming the boolean elements in a Java boolean array -
i'm stuck on simple thing. have array of booleans named "tags." it's of import me able access each element of array boolean:
public boolean energetic, calming, happy, sad; public boolean[] tracktags = {energetic, calming, happy, sad};
i pass in , assign boolean values tracktags array (let's [true, true, true, false]. so, when phone call tracktags[0], "true." however, when print "energetic," - should same tracktags[0], value false. know booleans initialize false, when switch values in tracktags array "true," shouldn't named elements alter well?
second question: what's way me interact boolean variable names? specifically, if pass in string[] [happy, sad], , want switch boolean[] values corresponding names in string array, feasible? have no problem looping through elements of both arrays, can't compare string boolean value.
all in all, there improve way interact boolean names? i'm confused this.
it sounds more want map
. allow name values mapping name key
value
.
example
map<string, boolean> tracktags = new hashmap<string, boolean>(); tracktags.put("energetic", false);
note
just code readability, i'd set names in final fields..
public static final string energetic = "energetic";
and access this..
tracktags.get(energetic);
that way if need alter name, alter in 1 place , not on show.
java arrays
No comments:
Post a Comment