Home > Java > Javagetting Started > How to create an array in java

How to create an array in java

王林
Release: 2020-10-20 10:13:14
Original
50664 people have browsed it

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];].

How to create an array in java

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, …};
Copy after login

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, …};
Copy after login

3. Specify the number of elements when declaring the array Then assign value

int[] arr1= new int[3];
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template