1. Overview
In this tutorial, We'll how to use boolean array and how to initialize boolean array in java.
Boolean array is used to store the only boolean values. boolean value can be either true or false.
The default value of boolean type is false and the default value of the primitive boolean array is false for all indexes.
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. Creating boolean Array
The boolean array can be created with empty curly braces. That means a boolean array is created with zero values in it.
Secondly, the boolean array is created with crurly braces with values inside it.
The third one is creating the boolean array with a new keyword and the size has to be defined at the time of declaration.
Example
package com.javaprogramto.arrays.booleanarray; public class BooleanArrayExample { public static void main(String[] args) { boolean[] array1 = {}; boolean[] array2 = { false, true, true }; boolean[] array3 = new boolean[5]; } }
3. Setting Values Into Boolean Primitive And Wrapper Array
Setting the values can be done while declaring the array or after creation of array uisng for loop or any other way.
Example
package com.javaprogramto.arrays.booleanarray; public class BooleanArrayExample2 { public static void main(String[] args) { // way 1 boolean[] array1 = { false, true, true }; // way 2 boolean[] array2 = new boolean[4]; array2[0] = true; array2[1] = false; array2[2] = true; array2[3] = false; // way 3 boolean[] array3 = new boolean[5]; for (int i = 0; i < array3.length; i++) { array3[i] = i % 2 == 0; } } }
4. Accessing Boolean Array Values
We have to use only the index of the boolean array to get the values of it. If you want to get the specific index value then you can psss that index to the array.
We can use the for loop or foreach to iterate the whole boolean array all values.
Look at the below examples.
Example
System.out.println("array1[0] value - " + array1[0]); System.out.println("array2[1] value - " + array2[1]); System.out.println("array3 last index value - " + array3[array3.length - 1]); System.out.println("\nboolean array 3 all values using foreach "); for(boolean b : array3) { System.out.println(b); }
Output
array1[0] value - false array2[1] value - false array3 last index value - true boolean array 3 all values using foreach true false true false true
5. Verifying Default Value of Boolean Array
Boolean array is initialized with the false as default value. This is the default value gets set to the boolean array in java.
Let us verify the what is the default value of boolean array in java.
Example
package com.javaprogramto.arrays.booleanarray; public class BooleanArrayExample4 { public static void main(String[] args) { boolean[] array4 = new boolean[4]; System.out.println("Checking the default values of boolean array 4 with for each loop"); for (boolean b : array4) { System.out.println(b); } } }
Output
Checking the default values of boolean array 4 with for each loop false false false false
6. Setting Default Values as True in Boolean Array
As we know that default value of boolean array is false. but in some cases, we may need it to be as true rather false.
In this case, we need to use the Arrays.fill() method after delcaring the array with the new keyword.
And also setting to true can be done with the simple for loop.
Example
public class BooleanArrayExample5 { public static void main(String[] args) { boolean[] array5 = new boolean[5]; System.out.println("Default value of array5 at index 1 is - " + array5[1]); System.out.println("Setting to true for all indexes of array 5 using simple for loop"); for (int i = 0; i < array5.length; i++) { array5[i] = true; } System.out.println("Now Default value of array5 at index 1 is - " + array5[1]); } }
Output
Default value of array5 at index 1 is - false Setting to true for all indexes of array 5 using simple for loop Now Default value of array5 at index 1 is - true
Next, example is on Arrays.fill(array, true)
package com.javaprogramto.arrays.booleanarray; import java.util.Arrays; public class BooleanArrayExample6 { public static void main(String[] args) { boolean[] array6 = new boolean[5]; System.out.println("Default values of array5 are " + Arrays.toString(array6)); Arrays.fill(array6, true); System.out.println("New Default values of array5 are " + Arrays.toString(array6)); } }
Output
Default values of array5 are [false, false, false, false, false] New Default values of array5 are [true, true, true, true, true]
7. Boolean Array Example
A simple program to create the boolean array and assing the values to it in single example.
import java.util.Arrays; public class BooleanArrayExample7 { public static void main(String[] args) { boolean[] array1 = {}; System.out.println("array1 length - " + array1.length); boolean[] array2 = new boolean[2]; System.out.println("array2 values - " + Arrays.toString(array2)); for (int i = 0; i < array2.length; i++) { array2[i] = true; } System.out.println("New array2 values - " + Arrays.toString(array2)); System.out.println("Acccessing array values by index - " + array2[1]); boolean[] array7 = new boolean[5]; System.out.println("Default values of array7 are " + Arrays.toString(array7)); Arrays.fill(array7, true); System.out.println("New Default values of array7 are " + Arrays.toString(array7)); } }
Output
array1 length - 0 array2 values - [false, false] New array2 values - [true, true] Acccessing array values by index - true Default values of array7 are [false, false, false, false, false] New Default values of array7 are [true, true, true, true, true]
8. Conclusion
In this article, we've seen all cocepts of boolean array in java with examples.
No comments:
Post a Comment
Please do not add any spam links in the comments section.