Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Boolean Arrays Be Initialized in a For Loop?

Tags:

Just found this SO question that happened to solve my problem with initializing a Boolean array initializing a boolean array in java. However, while it gave me code that will work, the asker wasn't trying the code that I was running that wasn't working, and I'd actually like to know why it doesn't work. This was the code I was trying:

Boolean[] array = new Boolean[5];
for(Boolean value : array) {
    value = false;
}

This is the functional code from that other question:

Boolean[] array = new Boolean[5];
Arrays.fill(array, Boolean.FALSE);

I'm just curious why the for loop approach doesn't work?

like image 568
sage88 Avatar asked Dec 06 '13 06:12

sage88


People also ask

How do you initialize a boolean array?

We can initialize a boolean array using the fill() method. The fill() method sets the static value of all array elements from the beginning index to the end index.

Can you use booleans in for loops?

A Boolean condition is either true or false. The program stays in the loop so long as the Boolean condition is true (1).

Can boolean be initialized?

The boolean array can be used to store boolean datatype values only and the default value of the boolean array is false. An array of booleans are initialized to false and arrays of reference types are initialized to null. In some cases, we need to initialize all values of the boolean array with true or false.

How do you initialize a boolean array to true?

Boolean array references are initialized with null values. In some cases, the default value is false is not useful. To make the default value true, we need to use the Arrays. fill() method.


2 Answers

Boolean[] array = new Boolean[5];
for(Boolean value : array) {
    value = false;
}

The java enhanced for loop uses an iterator to go through the array. The iterator returns a reference to the object, but java passes the reference by value, so you are unable to change what the reference points to, which is what you are trying to do with value = false.

EDIT:
As it turns out, for a normal array, instead of converting to a List and using an iterator, java does the following:

for (int i = 0; i < array.length; i++) 
{
    Boolean value = array[i]; //here's how we get the value that's referred to 
    ...                       //in the enchanced for loop  
}

While we are not using an iterator, the fact that Java passes references by value still explains what's going on here.
END of EDIT

If this were an array of objects with certain instance members, you would be able change said members, but not what the object, itself, references.

As others have suggested, to get around this, simple use a regular for loop and manually assign values to indexed slots in the array, ie:

Boolean[] b_values = new Boolean[5];
for(int i = 0; i < b_values.length; i++) 
{
    b_values[i] = Boolean.FALSE; 
}
like image 79
Steve P. Avatar answered Oct 19 '22 22:10

Steve P.


The reason why the code wasn't running is because when you mentioned that

Boolean[] array = new Boolean[5];
for(Boolean value : array) {
    value = false;
}

You are actually creating a new reference type called "Array of Boolean" and it contains only references to the five objects of Boolean class but the object doesn't exists as you haven't created them.

While in the second code

Boolean[] array = new Boolean[5];
Arrays.fill(array, Boolean.FALSE);

You are using cached object of Boolean class and adding it to the array you created using java.util.Arrays class. Boolean is a wrapper class in java and as only two possible values can be possible either true and false to avoid the overhead in creating them java already creates them for you and make them available for ready use.

like image 40
Bilbo Baggins Avatar answered Oct 19 '22 22:10

Bilbo Baggins