Surprised that this wasn't already posted. I'm making a for loop, and its worked before but for some reason I can't find the length of a boolean
array.
for(int z = 0; z < keyIsFound.length(); z++){
//do something
}
Surprised that this wasn't already posted. I'm making a for loop, and its worked before but for some reason I can't find the length of a boolean
array.
for(int z = 0; z < keyIsFound.length(); z++){
//do something
}
For arrays, their lengths are fixed when we create them.
If you want to get the length of any array, use .length
.
.length = to get the length for arrays
.length() = to get the length of Strings
For array the length
is a property - not a method. You have to write keyIsFound.length
. Array is a fixed sized data structure when you create an array like -
int[] nums = new int[10];
You actually fixed it length
too.
length
is a field, not a method.
Use something like for (int z = 0; keyIsFound != null && z < keyIsFound.length; ++z){
instead: i.e. drop the parentheses. Note my null
check, which you should consider incorporating either with the for
loop, or, better still, in a containing if
.
(I like to use ++z
rather than z++
as I'm an old-fashioned cat).