在Java布尔数组中命名布尔元素

时间:2021-05-29 21:22:45

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?

我传入并为trackTags数组分配布尔值(让我们说[true,true,true,false]。所以,当我调用trackTags [0]时,我得到“真实”。但是,当我打印“精力充沛”时 - 它应该与trackTags [0]相同,值总是为false。我知道布尔值初始化为false,但是当我在trackTags数组中切换一些值为“true”时,命名元素不应该也会改变吗?

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.

第二个问题:与布尔变量名交互的好方法是什么?具体来说,如果我传入一个String [] [happy,sad],然后只想切换对应于我的String数组中名称的boolean []值,这是否可行?我在循环遍历两个数组的元素时没有问题,但我显然无法将字符串与布尔值进行比较。

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

总而言之,是否有更好的方式与布尔名称进行交互?我真的很困惑。

3 个解决方案

#1


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.

这样,如果您需要更改名称,您可以在一个地方更改它,而不是整个节目。

#2


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?

我传入并为trackTags数组指定布尔值。但是,当我打印“精力充沛”时,值总是假的。我知道布尔值初始化为false,但是当我在trackTags数组中为“true”切换一些值时,命名元素不应该也会改变吗?

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.

您无法通过名称获取变量,甚至不能使用反射(不要将变量与类的字段混淆)。请改用Map ,boolean>

#3


2  

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

使用Map的另一种方法是定义一组单独的枚举。

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.

*我想补充一点,因为可定制性问题,这种方法可能不可取。例如,如果您希望枚举引用另一个从0偏移的数组的元素。

#1


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.

这样,如果您需要更改名称,您可以在一个地方更改它,而不是整个节目。

#2


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?

我传入并为trackTags数组指定布尔值。但是,当我打印“精力充沛”时,值总是假的。我知道布尔值初始化为false,但是当我在trackTags数组中为“true”切换一些值时,命名元素不应该也会改变吗?

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.

您无法通过名称获取变量,甚至不能使用反射(不要将变量与类的字段混淆)。请改用Map ,boolean>

#3


2  

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

使用Map的另一种方法是定义一组单独的枚举。

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.

*我想补充一点,因为可定制性问题,这种方法可能不可取。例如,如果您希望枚举引用另一个从0偏移的数组的元素。