Home  >  Article  >  Java  >  How does java define an array?

How does java define an array?

青灯夜游
青灯夜游Original
2019-11-15 14:41:522604browse

How does Java define an array? The following article will introduce to you how to define arrays in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How does java define an array?

Array definition method one: dynamic method (specify the length of the array)

Format :

数组存储的数据类型[] 数组名字 = new 数组存储的数据类型[长度];
  • [] : Represents an array.

  • Array name: Give a variable name to the defined array, which meets the identifier specification. You can use the name to operate the array.

  • #new: Keyword, keyword used to create an array. [Recommended learning: java course]

  • The data type stored in the array: See what data type the created array is used to store.

  • [Length]: The length of the array, indicating how many elements can be stored in the array container.

Example:

Define an array container that can store 3 integers. The code is as follows:

int[] arr = new int[3];

Array definition method 2 : Static mode (specify the elements of the array)

Format:

数据类型[] 数组名 = new 数据类型[]{元素1,元素2,元素3...};

Example: Definition of storing 1, 2, 3, 4, 5 integers array container.

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

Or omit the format: (You cannot declare first and then assign a value, you can only declare and assign a value at the same time)

数据类型[] 数组名 = {元素1,元素2,元素3...};

Example: Define an array container that stores 1, 2, 3, 4, 5 integers.

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

Note:

  1. #If you use a static method to create an array, the system will automatically calculate the length of the array based on the number of elements

  2. The length cannot be written in the square brackets on the right side of the array created in the static way

  3. The omitted format of the array created in the static way cannot be declared first and then assigned, it can only be declared At the same time, directly assign

The above is the detailed content of How does java define an array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to output in java?Next article:How to output in java?