如何在 Java 中创建一个数组?
在 Java 中,你可以使用 new 关键字像创建对象一样创建数组。使用 new 关键字在 Java 中创建数组的语法为 -
type[] reference = new type[10];
其中,
- type 是数组元素的数据类型。
- reference 是存储数组的引用。
并且,如果你想通过使用索引逐个向所有元素赋值来填充数组 -
reference [0] = value1; reference [1] = value2;
例如,如果你想创建一个包含 5 个元素的整数数组,可以使用 new 关键字来创建 -
int[] myArray = new int[5];
You can populate the array element by element using the array index:
myArray [0] = 101;
myArray [1] = 102;
You can also create and initialize an array directly using the flower brackets ({}).
int [] myArray = {10, 20, 30, 40, 50}
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP