Daybreak
Daybreak

Reputation: 165

How to check ALL elements of a boolean array are true

I have a boolean array whose size depends on the size of a randomly selected string.

So I have something like this:

boolean[] foundLetterArray = new boolean[selectedWord.length()];

As the program progresses, this particular boolean array gets filled with true values for each element in the array. I just want to print a statement as soon as all the elements of the array are true. So I have tried:

if(foundLetterArray[selectedWord.length()]==true){
    System.out.println("You have reached the end");
}

This gives me an out of bounds exception error. I have also tried contains() method but that ends the loop even if 1 element in the array is true. Do I need a for loop that iterates through all the elements of the array? How can I set a test condition in that?

Upvotes: 5

Views: 30912

Answers (8)

Paul Boddington
Paul Boddington

Reputation: 37645

There is a Java 8 one-liner for this:

boolean allTrue(boolean[] arr) {
    return IntStream.range(0, arr.length).allMatch(i -> arr[i]);
}

Upvotes: 4

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

boolean[] foundLetterArray = new boolean[5]; The memory allocation for the abow array is like

      foundLetterArray[0],foundLetterArray[1],foundLetterArray[2],foundLetterArray[3],foundLetterArray[4] 

Array index starts with 0 and the total memory count is 5 and the last array index is 4.

You are trying to get index 5 that is foundLetterArray[5] which does not exist. That's why you are getting the ArrayIndexOutofBoundsException

   if(foundLetterArray[selectedWord.length()-1]==true){
      System.out.println("You have reached the end");
   }

Upvotes: 1

Peter Walser
Peter Walser

Reputation: 15706

Using the enhanced for loop, you can easily iterate over an array, no need for indexes and size calculations:

private static boolean allTrue (boolean[] values) {
    for (boolean value : values) {
        if (!value)
            return false;
    }
    return true;
}

Upvotes: 12

user1543051
user1543051

Reputation:

You do the check only for last element in the array ( and do it wrong, above is described why ).

In order to get if arrays contains only true values you should check all of them.

boolean allAreTrue = true;
for (boolean val : foundLetterArray) {
  allAreTrue = allAreTrue && val;
}

// at this line allAreTrue will contain true if all values are true and false if you have at least one "false"

Upvotes: 0

Tom McIntyre
Tom McIntyre

Reputation: 3699

do this

public boolean allTrue(boolean[] array) {
    for (boolean b : array) {
        if (!b) {
           return false;
        }
    }
    return true;
}

There is no 'one line' way of knowing whether all of the elements of an array meet a certain condition (there are libraries that take care of the looping for you, but you still need to write the condition). Querying array[x] will only tell you about the xth item in that array, so for your question you need to check every item.

Also, as other people have pointed out, array indices are 0-based, so the first element is at array[0] and the last at array[array.length() - 1].

My example uses an alternate looping construct known as for-each. This is appropriate when you don't need to modify the array contents, you only need to read from them. It avoids any messing around with indices.

Upvotes: 0

Satyam Koyani
Satyam Koyani

Reputation: 4274

Array index start from 0 so last index is always 1 less then array length so here you are trying to access last index + 1 by doing foundLetterArray[selectedWord.length()] this so it is throuing ArrayIndexBoundEception use array.lastIndex() method or subtract 1 form length.

Implementing this foundLetterArray[selectedWord.length()-1] You must take care about one thing if your array does not contains any elements then selectedWord.length() return 0 and again you will get same exception so Its good to check lengh before doing this foundLetterArray[selectedWord.length()-1].

Upvotes: 0

user2742438
user2742438

Reputation:

Indices in java, as well as most programming languages, are 0-based, meaning that individual elements in an array with n elements have indices 0, 1, 2, ..., n-1. The last element in an array is always at index array.length - 1.

Upvotes: 0

Nargis
Nargis

Reputation: 4787

Arrays in Java starts from index 0 and last index is always [array.length()-1]

As you are checking for foundLetterArray[selectedWord.length()] ,its giving you a array out of Bound Exception try foundLetterArray[selectedWord.length()-1]

Like:

if(foundLetterArray[selectedWord.length()-1]){
System.out.println("You have reached the end");
}

Upvotes: 0

Related Questions