Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lingcoder committed May 24, 2020
1 parent fae6ef8 commit 34a061b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/book/06-Housekeeping.md
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ a1[4] = 6;

### 动态数组创建

如果在编写程序时,不确定数组中需要多少个元素,那么该怎么办呢?你可以直接使用 **new** 在数组中创建元素。下面例子中,尽管创建的是基本类型数组,**new** 仍然可以工作(不能用 **new** 创建单个的基本类型数组)
如果在编写程序时,不确定数组中需要多少个元素,可以使用 **new** 在数组中创建元素。如下例所示,使用 **new** 创建基本类型数组。**new** 不能创建非数组以外的基本类型数据

```java
// housekeeping/ArrayNew.java
Expand All @@ -1294,7 +1294,7 @@ length of a = 18
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
```

数组的大小是通过 `Random.nextInt()` 随机确定的,这个方法会返回 0 到输入参数之间的一个值。 由于随机性,很明显数组的创建确实是在运行时进行的。此外,程序输出表明,数组元素中的基本数据类型值会自动初始化为空值(对于数字和字符是 0;对于布尔型是 **false**)。`Arrays.toString()`**java.util** 标准类库中的方法,会产生一维数组的可打印版本。
数组的大小是通过 `Random.nextInt()` 随机确定的,这个方法会返回 0 到输入参数之间的一个值。 由于随机性,很明显数组的创建确实是在运行时进行的。此外,程序输出表明,数组元素中的基本数据类型值会自动初始化为默认值(对于数字和字符是 0;对于布尔型是 **false**)。`Arrays.toString()`**java.util** 标准类库中的方法,会产生一维数组的可打印版本。

本例中,数组也可以在定义的同时进行初始化:

Expand Down

0 comments on commit 34a061b

Please sign in to comment.