This chapter highlights
1. Mastering the grammatical structure of an array
2. Precautions for using arrays
(1) The subscript of an array must start from 0
(2) The Declaration and assignment of an array must be done together.
1 int []sroce; 2 score={1,2,3,4};//this is wrong.
Usage of 3.arrays.sort ()
4. Use of the length of the array
The difficulty of this chapter
1. Use of arrays
2. Inserting elements into an array (optional)
1. About arrays
1.1 An array is a set of variables of the same type
1.2 When you find that you need to store more than one piece of data, consider using an array
1.3 When an array is created, it will open up a contiguous space in memory
2. How to use arrays
2.1 data type [] Array name = new data type [array length]; (although it is not wrong to put the brackets in the array name, I recommend that you write the following rules)
2.2 data type [] Array name = {value, value};
2.3 data type [] Array name = new data type []{value, value, value};(Note the length cannot be written in parentheses)
2.4 In the 2.1 syntax structure, the array length can be substituted with variables such as:
1 New Scanner (system.in); 2 int size = scanner.nextint (); 3 int New int [Size];
3. Data processing of arrays
The 3.1 array is usually accessed using a small label, and the subscript starts at 0. (subscript is equivalent to serial number)
3.2 Array name. Length is used to get array lengths
3.3 How arrays are assigned: array name [subscript] = value to be assigned
3.4 Typically iterate through the elements of an array
4. About array length vs. array default initial values
4.1 Usage: array name. Length (this usage can be used to get the length of the array, remember that when you use this length to get the value of the array will be an error)
For example:
1 int New int []{10,20,30}; 2 int size = number.length; // at this point the value of size is 3 3 System.out.println (Number[size]); // This is going to be a bad list of cross-border errors.
4.2 When arrays are created successfully, the system will initialize their default values if they are not assigned a value
An array of type int has an initial value of 0
The initial value of the double type is 0.0
The initial value of the char type is ' \u0000 ' (corresponds to [])
The initial value of the Boolean type is False
5. About array Sorting
Code: Arrays.sort ();
5.1 Note this is sort ascending
5.2 Similar to the use of scanner, need to introduce import.java.util.Arrays;
5.3 About Array Descending (idea: Reverse the sorted array output)
1 arrays.sort (number); 2 for (int// Note Be sure to reduce the length by 13System.out.println (Number[i]); }
6. Take the maximum and minimum values in the array
6.1 The simplest method is to call Arrays.sort (the array name) and take the first and last element
6.2 If you realize the idea:
Define a variable to take the maximum (or minimum), the default number is the largest element of the first element, and then iterate over the array element if there is greater than the defined value of the variable, then the assignment
The simple generalization is: Who can do who?
1 int max = number[0]; 2 for (int i = 1; i<number.length;i++) {3 if (number[i]>max) {4 max = Number[i]; 5 }6 }
ACCP8.0 Course first semester-about Java arrays