How to Get the Length of a Boolean Array in Java
In Java programs, you can store many data types within array structures, including booleans. You can use array variables to store sequences of both primitive type and object type booleans. Usually, once you have an array structure in a program, you will need to process it, such as looping through it. In order to carry such processes out, you typically first need to establish the length of the array. In Java, an array stores its own length as an integer variable. To find the length, you need to access this variable.
Instructions
-
-
1
Prepare your array variable. If you do not already have one in your program, create an array to store primitive type booleans as follows:
boolean[] myBooleans = {true, true, false, true, false, false, true};To declare an array for storing object type booleans, use the following syntax:
Boolean[] myBooleanObjects = {Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, Boolean.TRUE};These arrays are for demonstration. If you already have an array in your program, you can use it. This code declares each boolean array variable and instantiates it with values.
-
2
Find the length of your array. The array type in Java stores length as a field. To access this value, use the following syntax, specifying the name of the array and the length variable:
myBooleans.length;This code accesses the array length for the specified variable. Notice that the code specifies the length without brackets, as the length is a field, not a method, as in some other data types. This code may access the length, but at the moment it does nothing with it.
-
-
3
Store the length of your array in a variable. The length of an array is stored as an integer type. Use the following code syntax to store your array length as a primitive type integer for later use in your program:
int boolLength = myBooleans.length;You can then refer to this variable wherever you need to use the array length. For the object type boolean array the process is the same:
int booleanLength = myBooleanObjects.length; -
4
Use your boolean array length within your program. You can use the array length directly as in the following for loop:
for(int b=0; b<boolLength; b++)
{ System.out.println(myBooleans[b]); }The length in this case forms part of the conditional test determining whether Java should execute the body of the loop on each iteration. The final array element position has an index that is one less than the array length, so if you try to access the position at the value represented by the length integer, your code will throw an exception and possibly crash. This loop writes out each array element in turn, stopping when it reaches the end of the structure.
-
5
Save your Java file. Compile and run your program to test its output. Experiment with the code to make sure you understand the process. Try altering the "for" loop code to read past the end of the array and see the error messages your development environment outputs. Use the array length variable in other processing code to suit your project.
-
1
Tips & Warnings
Arrays can be difficult to get used to, but they are predictable once you do.
Your programs should avoid using object types like the boolean wrapper class where possible, as they use unnecessary resources.