4

I'm stuck on a simple thing. I have an array of booleans named "tags." It's important to me to be able to access each element of the array by boolean:

public boolean energetic, calming, happy, sad;
public boolean[] trackTags = {energetic, calming, happy, sad};

I pass in and assign boolean values to the trackTags array (let's say [true, true, true, false]. So, when I call trackTags[0], I get "true." However, when I print "energetic," - which should be the same as trackTags[0], the value is always false. I know booleans initialize false, but when I switch some values in the trackTags Array for "true," shouldn't the named elements change as well?

Second question: what's a good way for me to interact with boolean variable names? Specifically, if I pass in a String[] [happy, sad], and then want to switch only the boolean[] values corresponding to names in my String array, is this feasible? I have no trouble looping through the elements of both arrays, but I obviously can't compare a string to a boolean value.

All in all, is there a better way to interact with boolean names? I'm really confused about this.

Maxim P
  • 432
  • 4
  • 15
  • Your question is an mix of [this](http://stackoverflow.com/questions/6729605/assigning-variables-with-dynamic-names-in-java) and [this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value). – Sotirios Delimanolis Nov 10 '14 at 21:37
  • Use an `EnumSet` maybe? – khelwood Nov 10 '14 at 21:38
  • 3
    `shouldn't the named elements change as well?` No, they shouldn't. `boolean` is a primitive java type. It means that a boolean variable does not store a reference to the boolean value, but the value itself. When you make this: `{energetic, calming, happy, sad}` you are copying the value of these four boolean variables to the array, but no the reference to them. So, they are different. If you were handling with *objects*, instead of a *primitive type*, then the reference would not be lost and your code would work. – felipeek Nov 10 '14 at 21:44

3 Answers3

11

It sounds more like you want something like a Map. This will allow you to effectively name the values by mapping the name as a key to a value.

Example

Map<String, Boolean> trackTags = new HashMap<String, Boolean>();

trackTags.put("energetic", false);

NOTE

Just for good code readability, I'd also put the names in some final fields..

public static final String ENERGETIC = "energetic";

and access it like this..

trackTags.get(ENERGETIC);

That way if you need to change the name, you change it in one place and not all over the show.

christopher
  • 24,892
  • 3
  • 50
  • 86
5

I pass in and assign boolean values to the trackTags array. However, when I print "energetic," the value is always false. I know booleans initialize false, but when I switch some values in the trackTags Array for "true," shouldn't the named elements change as well?

No. boolean is a primitive type, so you're not storing a reference of the value of the boolean, instead a direct boolean value.

what's a good way for me to interact with boolean variable names?

You cannot fetch a variable by its name, not even by using reflection (do not confuse variable with field of a class). Use a Map<String, Boolean> instead.

Luiggi Mendoza
  • 81,685
  • 14
  • 140
  • 306
2

An alternative to using a Map would be defining a separate set of enums.

enum tagNames = {energetic, calming, happy, sad};
boolean tags[] = new boolean[4];

Then you can then modify your current boolean array like normal:

for(int i = 0; i < tags.length; i++){
    if (tags[i] == 1)
        trackTags[i] = true;
}

But you can also look up specific values using your enums:

tags[tagNames.energetic.ordinal()] //returns boolean value at [0]

*I wanted to add that this method is probably not advisable because of customizability issues. One example is if you wanted your enums to refer to elements of another array that are offset from 0.

HedonicHedgehog
  • 582
  • 1
  • 4
  • 16