Java Chapter 7 Array Knowledge Points

Basic elements of an array:

(1) Identifier. First of all, like variables, in computers, arrays also have a name, called an identifier, which is used to distinguish different arrays.

(2) Array elements. When the array name, that is, the array identifier, is given, data is to be stored in the array, and these data are called array elements.

(3) Array subscripts. In an array, in order to get the elements of the array correctly, they need to be numbered, so that the computer can access according to the number, this number is called the array subscript.

(4) Element type. The array elements stored in the array should be of the same data type. For example, the grades of students can be stored in an array, and the grades of each student can be stored in an integer variable, so the type of its elements is called an integer.


Use the Arrays class to manipulate arrays:

(1) Compare two arrays for equality

                The equals() method of the Arrays class is used to compare two arrays for equality. This method returns true only when the lengths of the two arrays are equal and the elements in the corresponding positions are also equal; otherwise, it returns false.

(2) Arrange the elements of the array in ascending order

                The sort() method of the Arrays class sorts the elements of an array in ascending order, that is, in ascending order.

(3) Convert the array to a string

                The Arrays class provides a method that specifically outputs the contents of the array - the toString() method. This method is used to convert an array to a string. Sequentially connect multiple array elements together, and multiple array elements should be separated by English and spaces. Using this method, the individual array elements can be clearly seen.

(4) Assign all elements of the array to the same value

                The fill(array, val) method of the Arrays class is used to assign all elements of the array array to val.

(5) Copy the array into a new array with the length of the set value

                Initialize a one-dimensional array of integers, and use the copyOf() method of the Arrays class to copy the array into a new array of the specified length.

(6) Query the subscript of the element in the array

                The binarySearch() method of the Arrays class is used to query the index of the array element in the array. Calling this method requires that the elements in the array are already in ascending order in order to get the correct result.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940578&siteId=291194637