1. Overview
1.概述
Boolean is a fundamental data type in Java. Usually, it can have only two values, true or false.
布尔型是Java中的一个基本数据类型。通常,它只能有两个值,true或false。
In this tutorial, we’ll discuss how to initialize an array of boolean values.
在本教程中,我们将讨论如何初始化一个布尔值的数组。
2. Introduction to the Problem
2.对问题的介绍
The problem’s pretty straightforward. Simply put, we want to initialize an array of boolean variables with the same default value.
这个问题很简单。简单地说,我们想用相同的默认值初始化一个布尔变量数组。
However, Java has two “different” boolean types, the primitive boolean and the boxed Boolean. Therefore, in this tutorial, we’ll cover both cases and address how to initialize an array of boolean and Boolean.
然而,Java 有两种 “不同的 “布尔类型,primitive boolean和boxed Boolean。因此,在本教程中,我们将涵盖这两种情况,并解决如何初始化boolean和Boolean的数组。
Also, for simplicity, we’ll use unit test assertions to verify if our array initializations work as expected.
另外,为了简单起见,我们将使用单元测试断言来验证我们的数组初始化是否按预期工作。
So next, let’s start with the primitive boolean type.
那么接下来,让我们从原始的boolean类型开始。
3. Initializing a Primitive boolean Array
3.初始化一个原始的boolean阵列
In Java, a primitive variable has a default value. For example, a primitive int variable’s default value is 0, and a primitive boolean variable will hold false by default.
在Java中,一个原始变量有一个默认值。例如,一个原始的int变量的默认值是0,而一个原始的boolean变量的默认值是false。
Therefore, if we want to initialize a boolean array with all false, we can simply create the array without setting the values.
因此,如果我们想用所有的false来初始化一个boolean数组,我们可以简单地创建数组而不设置值。
Next, let’s create a test to verify it:
接下来,让我们创建一个测试来验证它。
boolean[] expected = { false, false, false, false, false };
boolean[] myArray = new boolean[5];
assertArrayEquals(expected, myArray);
If we run the test above, it passes. As we can see, boolean[] myArray = new boolean[5]; initializes five false elements in the boolean array.
如果我们运行上面的测试,它通过了。我们可以看到,boolean[] myArray = new boolean[5];在boolean数组中初始化了五个false元素。
It’s worth mentioning that when we want to compare two arrays based on their values, we should use the assertArrayEquals() method instead of assertEquals(). This is because the array’s equals() method checks whether the references of two arrays are the same.
值得一提的是,当我们想要根据两个数组的值进行比较时,我们应该使用assertArrayEquals()方法,而不是assertEquals()/strong>。这是因为数组的equals()方法检查两个数组的引用是否相同。
So, initializing an array of primitive false is easy, as we can make use of the boolean‘s default value. However, sometimes, we may want to create an array of true values. If this is the case, we must somehow set the values to true. Of course, we can set them individually or within a loop. But the Arrays.fill() method makes the work easier:
所以,初始化一个原始false的数组很容易,因为我们可以利用boolean的默认值。然而,有时我们可能想创建一个true值的数组。如果是这种情况,我们必须以某种方式将这些值设置为true。当然,我们可以单独或在一个循环中设置它们。但是Arrays.fill()方法使这项工作更加容易。
boolean[] expected = { true, true, true, true, true };
boolean[] myArray = new boolean[5];
Arrays.fill(myArray, true);
assertArrayEquals(expected, myArray);
Again, the test passes if we give it a run. So, we’ve got an array of true.
同样,如果我们让它运行,测试就会通过。所以,我们有一个true的数组。
4. Initializing a Boxed Boolean Array
4.初始化盒式Boolean阵列
So far, we’ve learned how to initialize an array of primitive boolean with true or false. Now, let’s look at the same operations on the boxed Boolean side.
到目前为止,我们已经学会了如何用true或false来初始化一个原始boolean数组。现在,让我们看看在盒式Boolean上的同样操作。
First, unlike a primitive boolean variable, a boxed Boolean variable doesn’t have a default value. if we don’t set a value to it, its value is null. Therefore, if we create an array of Boolean without setting elements’ values, all elements are nulls. Let’s create a test to verify it:
首先,与原始的boolean变量不同,盒式Boolean变量没有默认值。如果我们不给它设置一个值,它的值就是null。因此,如果我们创建一个Boolean数组,而不设置元素的值,那么所有的元素都是nulls。让我们创建一个测试来验证。
Boolean[] expectedAllNull = { null, null, null, null, null };
Boolean[] myNullArray = new Boolean[5];
assertArrayEquals(expectedAllNull, myNullArray);
If we want to initialize the Boolean array with true or false, we can still use the Arrays.fill() method:
如果我们想用true或false来初始化Boolean数组,我们仍然可以使用Arrays.fill()方法。
Boolean[] expectedAllFalse = { false, false, false, false, false };
Boolean[] myFalseArray = new Boolean[5];
Arrays.fill(myFalseArray, false);
assertArrayEquals(expectedAllFalse, myFalseArray);
Boolean[] expectedAllTrue = { true, true, true, true, true };
Boolean[] myTrueArray = new Boolean[5];
Arrays.fill(myTrueArray, true);
assertArrayEquals(expectedAllTrue, myTrueArray);
The test above passes when we run it.
当我们运行上面的测试时,它通过了。
5. Conclusion
5.总结
In this short article, we’ve learned how to initialize a boolean or Boolean array in Java.
在这篇短文中,我们已经学习了如何在Java中初始化一个boolean或Boolean数组。
As always, the full source code for the examples is available over on GitHub.
一如既往,例子的完整源代码可在GitHub上获得。。