Getting started with JavaSE 9: array of Basic Java syntax
Definition of an array
Arrays can be understood as a huge "box", which can store multiple data of the same type in order, for example, an array of the int type can be defined.
Scores stores scores of four students.
All elements in the array can be accessed by subscript. The subscript starts from 0. For example, you can use scores [0] to obtain the first element 76 in the array,
Scores [2] can get the third element 92.
Perform the following four steps to operate the array:
1 declare an array
Syntax: Data Type [] array name; or data type array name [];
The array name can be any valid variable name, for example:
2. Allocate space
Simply put, it is to specify the maximum number of elements that can be stored in an array.
Syntax: array name = new data type [array length];
The length of the array is the number of elements that can be stored in the array, for example:
We can also merge the above two steps to allocate space for the array while declaring it, such:
3 assignment
After the space is allocated, you can put data into the array. All elements in the array are accessed by subscript. For example, you can store student scores in the scores array:
4. process data in the array
We can operate and process the assigned array, such as getting and outputting the values of elements in the array:
In Java, another method is provided for directly creating arrays. It combines declared arrays, allocated spaces, and assigned values, for example:
It is equivalent:
3. Use loops to operate arrays in Java
In actual development, we often use loops to control the operations of array members. For example:
Running result:
The array name. length is used to obtain the length of the array.
Note the following:
The subscript of array 1 starts from 0. Therefore, scores [3] indicates 4th elements in the array, rather than 3rd elements;
2. The range of objects under the array is 0 to the length of the array-1. If the object is accessed across the border, an error is returned. For example:
The error message above indicates that the array subscript exceeds the range, that is, the array access is out of bounds. In the above Code, create an array with a length of 2.
The subscript range of the group is 0 to 1, and the subscript in the program appears 2, that is, scores [2], exceeds the range, causing the array access to cross-border.
4. array operations using the Arrays class
The Arrays class is a tool class provided by Java in the java. util package. This class contains some methods for directly operating arrays, such
Sort and search the current array.
Common Methods in Arrays:
1. Sort
Syntax: Arrays. sort (array name );
You can use the sort () method to sort the array. You only need to put the array name in the brackets of the sort () method to sort the Array (
In ascending order), such:
Running result:
2. convert an array to a string
Syntax: Arrays. toString (array name );
You can use the toString () method to convert an array into a string. This method concatenates multiple array elements in order and uses
Separated by commas (,), for example:
Running result: the elements in the output array nums are: [,]
The Arrays class also provides many other methods to operate Arrays.
5. Use foreach to operate Arrays
Foreach is not a keyword in Java. It is a special simplified version of the for statement. It is easier and more convenient to traverse arrays and collections. From English
The meaning of foreach, that is, "for each", is literally understood. How can we make the foreach statement?
Syntax:
We use the for and foreach statements to traverse the array respectively:
Running result:
Vi. Two-dimensional array in Java
The so-called two-dimensional array can be simply understood as a "special" one-dimensional array, which stores a one-dimensional array in each array space.
To use a two-dimensional array, follow these steps:
1. Declare the array and allocate space
Or
For example:
2 Assignment
The assignment of two-dimensional arrays is similar to that of one-dimensional arrays. You can assign values one by one using subscript. Note that the index starts from 0:
You can also assign values to an array while declaring it.
For example:
3. Process Arrays
The access to a two-dimensional array is the same as the output of an array of the same dimension, but there is an additional subscript. In loop output, a loop needs to be embedded in it, that is
Use a double loop to output every element in a two-dimensional array. For example:
Running result:
Note: when defining a two-dimensional array, you can specify only the number of rows, and then specify the number of columns for each row. If the number of columns in each row is not
In the same way, an irregular two-dimensional array is created, as shown below:
The running result is: