How to define arrays in Java

In Java, arrays can be defined using the following syntax:

// 定义数组类型 变量名[] = new 数组类型[数组长度];
int[] numbers = new int[5];

The above code defines an integer array named numbers with a length of 5. The declaration and initialization can also be written separately.

int[] numbers;
numbers = new int[5];

In addition to creating array objects using the new keyword, you can also use simplified syntax to initialize arrays.

// 定义数组并初始化其中的元素
int[] numbers = {1, 2, 3, 4, 5};

This defines an integer array containing 5 elements and initializes it with values 1, 2, 3, 4, 5.

bannerAds