How to create an array in java: declare the array name to open up space and assign a value, such as [int[] arr;arr = new int[]{1,2,3, …};]. You can also specify the number of elements and then assign values when declaring the array, such as [int[] arr1= new int[3];].
There are roughly three ways to create an array in Java
Explanation: Here, int is used as the data type and arr is used as the array name to demonstrate
(Recommended tutorial: java course)
1. Declaration and assignment
int[] arr = {1,2,4, …};
Note that the curly braces here are not statement blocks, and after the curly braces The semicolon cannot be omitted,...it is not an element, it means you can specify multiple elements
2. Declare the array name to open up space and assign the value
int[] arr; arr = new int[]{1,2,3, …};
3. Specify the number of elements when declaring the array Then assign value
int[] arr1= new int[3];
Note: The maximum element subscript is 2, and all element values are 0
Assignment generally uses a for loop
4. Above Create a multi-dimensional array based on The number of elements in the first square bracket cannot be omitted
"new data type []{}" When creating an array, the curly brackets can be omitted, but the array must be filled in "[ ]" Number
(Related recommendations:
java introductory tutorial)
The above is the detailed content of How to create an array in java. For more information, please follow other related articles on the PHP Chinese website!