This article will introduce to you what an array is in Java. I hope it will be helpful to friends in need!
Arrays are one of the important data structures for every programming language. Of course, different languages implement and process arrays differently.
Arrays provided in the Java language are used to store fixed-size elements of the same type.
You can declare an array variable, such as numbers[100] instead of directly declaring 100 independent variables number0, number1, ...., number99.
Declare array variables
First, you must declare an array variable before you can use the array in the program. The following is the syntax for declaring array variables:
dataType[] arrayRefVar; // 首选的方法
or
dataType arrayRefVar[]; // 效果相同,但不是首选方法
Note: It is recommended to use the declaration style of dataType[] arrayRefVar to declare array variables. The dataType arrayRefVar[] style comes from the C/C language and is adopted in Java to allow C/C programmers to quickly understand the java language.
Examples
The following are code examples for these two syntaxes:
double[] myList; // 首选的方法
or
double myList[]; // 效果相同,但不是首选方法
Creating an array
Java language uses the new operator to create an array. The syntax is as follows:
arrayRefVar = new dataType[arraySize];
The above syntax statement does two things:
1. Use dataType[arraySize] to create an array.
2. Assign the reference of the newly created array to the variable arrayRefVar.
The declaration of array variables and the creation of an array can be completed in one statement, as shown below:
dataType[] arrayRefVar = new dataType[arraySize];
In addition, you can also create an array in the following way.
dataType[] arrayRefVar = {value0, value1, ..., valuek};
The elements of the array are accessed by index. Array indexing starts at 0, so index values range from 0 to arrayRefVar.length-1.
Related recommendations: "Java Tutorial"
The above is the detailed content of What is an array in JAVA?. For more information, please follow other related articles on the PHP Chinese website!