9

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
}
Razib
  • 10,965
  • 11
  • 53
  • 80
Muhammad Shahab
  • 119
  • 1
  • 1
  • 4

4 Answers4

11

For arrays, their lengths are fixed when we create them. If you want to get the length of any array, use .length.

Ankit Shubham
  • 2,989
  • 2
  • 36
  • 61
5

.length = to get the length for arrays
.length() = to get the length of Strings

Ruelos Joel
  • 2,209
  • 3
  • 19
  • 33
3

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.

Razib
  • 10,965
  • 11
  • 53
  • 80
0

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).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    I'd think it would be enough to check for `keyIsFound != null` at the very start, in a separate `if` clause. – glglgl Dec 09 '15 at 13:22
  • Very true, I could play the "compiler will optimise out card", but then I open myself to ridicule over using ++z. – Bathsheba Dec 09 '15 at 13:23