Home > Common Problem > body text

What are the definitions of arrays?

百草
Release: 2023-06-20 17:40:54
Original
3575 people have browsed it

What are the definitions of arrays?

1. Data type [] array name = new data type [length];

The first way to define an array: 1. Data type [] array name = new data type [length] This method gives the length of the array when defining it, but does not specify the value of each element. Defining an array in this way is equivalent to declaring an empty array with a fixed volume, so we need to assign values ​​to the elements in the array ourselves after definition.

For example:

int [] arr=new int[5];//Declare an array named arr with a length of 5.

arr[0]=1;//Assign a value to each element in the array through the subscript of the array.

arr[2]=5;

2. Data type [] array name = new data type [] {element, element, element};

Array definition The second method data type [] array name = new data type [] {element, element, element} This method does not give the length of the array when defining, but directly gives the value of each element in the array. Arrays declared in this way can be used directly without requiring us to assign values ​​to the elements again.

string [] arr=new string [] {"hello","world","message"};//Declares an array of string type named arr, containing three elements.

At this point we can use the elements in the array by adding the array name to the element subscript. For example, arr[0] is the first element "hello" in the array.

3. Data type [] array name = {element, element, element};

The third way to define an array is data type [] array name = {element, Element, element}; This definition is actually the abbreviation of the second definition. Using this definition allows us to write less code but achieve the same functionality.

The above is the detailed content of What are the definitions of arrays?. 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