How to define an array in Java?

In Java, you can define an array using the following syntax:

数据类型[] 数组名 = new 数据类型[数组长度];

The data type represents the type of elements in the array, the array name is the name you give to the array, and the array length indicates the number of elements in the array.

For example, the following code defines an integer array named “numbers” with a length of 5:

int[] numbers = new int[5];

You can also directly assign initial values to an array when defining it, for example:

int[] numbers = {1, 2, 3, 4, 5};

In this way, the array numbers contains 5 elements, which are 1, 2, 3, 4, and 5.

bannerAds