如何检查布尔数组的所有元素都是true

时间:2021-05-29 21:22:27

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?

这给了我一个越界异常错误。我也尝试过contains()方法,但即使数组中的1个元素为真,也会结束循环。我是否需要一个迭代遍历数组所有元素的for循环?我该如何设置测试条件?

8 个解决方案

#1


10  

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

使用增强的for循环,您可以轻松地遍历数组,无需索引和大小计算:

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

#2


2  

There is a Java 8 one-liner for this:

这有一个Java 8单线程:

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

#3


1  

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

boolean [] foundLetterArray = new boolean [5]; abow数组的内存分配就像

      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.

数组索引从0开始,总内存计数为5,最后一个数组索引为4。

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

您正在尝试获取不存在的foundLetterArray [5]的索引5。这就是你得到ArrayIndexOutofBoundsException的原因

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

#4


0  

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

Java中的数组从索引0开始,最后一个索引始终是[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]

当你检查foundLetterArray [selectedWord.length()]时,它会给你一个绑定异常的数组,试试findLetterArray [selectedWord.length() - 1]

Like:

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

#5


0  

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.

java中的索引以及大多数编程语言都是基于0的,这意味着具有n个元素的数组中的各个元素具有索引0,1,2,...,n-1。数组中的最后一个元素始终位于索引array.length - 1。

#6


0  

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.

数组索引从0开始,所以最后一个索引总是比数组长度少1,所以这里你试图通过执行foundLetterArray [selectedWord.length()]来访问最后一个索引+ 1,这样就可以通过使用array.lastIndex()方法来实现ArrayIndexBoundEception或者减1表格长度。

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

实现这个foundLetterArray [selectedWord.length() - 1]你必须要注意一件事,如果你的数组不包含任何元素,那么selectedWord.length()会返回0,你会得到同样的异常,所以在做之前检查lengh是好的这个foundLetterArray [selectedWord.length() - 1]。

#7


0  

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.

没有“一行”的方式来了解数组的所有元素是否满足某个条件(有些库可以为您处理循环,但您仍然需要编写条件)。查询array [x]只会告诉你该数组中的第x项,所以对于你的问题,你需要检查每一项。

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

另外,正如其他人所指出的,数组索引是从0开始的,所以第一个元素在数组[0],最后一个在数组[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.

我的例子使用了一个称为for-each的备用循环结构。当您不需要修改数组内容时,这是合适的,您只需要从中读取它们。它避免了对指数的任何混乱。

#8


0  

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"

//如果所有值都为真,那么allAreTrue将包含true,如果至少有一个“false”,则allAreTrue将包含false

#1


10  

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

使用增强的for循环,您可以轻松地遍历数组,无需索引和大小计算:

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

#2


2  

There is a Java 8 one-liner for this:

这有一个Java 8单线程:

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

#3


1  

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

boolean [] foundLetterArray = new boolean [5]; abow数组的内存分配就像

      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.

数组索引从0开始,总内存计数为5,最后一个数组索引为4。

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

您正在尝试获取不存在的foundLetterArray [5]的索引5。这就是你得到ArrayIndexOutofBoundsException的原因

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

#4


0  

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

Java中的数组从索引0开始,最后一个索引始终是[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]

当你检查foundLetterArray [selectedWord.length()]时,它会给你一个绑定异常的数组,试试findLetterArray [selectedWord.length() - 1]

Like:

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

#5


0  

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.

java中的索引以及大多数编程语言都是基于0的,这意味着具有n个元素的数组中的各个元素具有索引0,1,2,...,n-1。数组中的最后一个元素始终位于索引array.length - 1。

#6


0  

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.

数组索引从0开始,所以最后一个索引总是比数组长度少1,所以这里你试图通过执行foundLetterArray [selectedWord.length()]来访问最后一个索引+ 1,这样就可以通过使用array.lastIndex()方法来实现ArrayIndexBoundEception或者减1表格长度。

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

实现这个foundLetterArray [selectedWord.length() - 1]你必须要注意一件事,如果你的数组不包含任何元素,那么selectedWord.length()会返回0,你会得到同样的异常,所以在做之前检查lengh是好的这个foundLetterArray [selectedWord.length() - 1]。

#7


0  

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.

没有“一行”的方式来了解数组的所有元素是否满足某个条件(有些库可以为您处理循环,但您仍然需要编写条件)。查询array [x]只会告诉你该数组中的第x项,所以对于你的问题,你需要检查每一项。

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

另外,正如其他人所指出的,数组索引是从0开始的,所以第一个元素在数组[0],最后一个在数组[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.

我的例子使用了一个称为for-each的备用循环结构。当您不需要修改数组内容时,这是合适的,您只需要从中读取它们。它避免了对指数的任何混乱。

#8


0  

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"

//如果所有值都为真,那么allAreTrue将包含true,如果至少有一个“false”,则allAreTrue将包含false