Eighth Chapter
What is an array and how does it work?
Definition: A collection with the same data type
Function: Stores contiguous data of the same type
Ii. how to declare and define arrays in Java
L Declaration and definition of syntax:
data type [] array name; (int[] nums;)
or data type array name []; (int nums[];)
• Allocating space for an array
Array name =new data type [length of array]; (Nums=new int[5];)
L declare an array, allocating space at the same time
data type [] Array name = new data type [length of array];
Data type array name []=new data type [length of array];
(int[] nums=new int[5];) (int nums[]=new int[5];)
Third, how to read the elements in the array-by subscript (index) to get
Subscript (index) is starting from 0, the maximum subscript is an array length-1
Subscript out of Bounds: arrayindexoutofboundsexception
An array of type int, with a default value of 0 for each element
An array of type string, with a default value of NULL for each element
1. Get the value of a single element
Data type variable name = array name [subscript];
2. Iterate through each element in the array
The length of the array: array name. Length Property Automatic calculation
for (int i=0;i< array name. length; i++) {
Array name [i];
}
Iv. How to assign values to the elements in an array
Mode 1 (give array fixed value):
Declares an array, allocates space, and assigns values
(The length of the array is determined by the number of values)
Data type array name []=new data type []{value 1, value 2 ... Value n};
Or
data type [] Array name ={value 1, value 2 ... Value n};
Description: [] brackets can be placed before or after the array name
Mode 2: Dynamically assign values to the array (input from the keyboard)
Scanner input = new Scanner (system.in);
for (int i=0;i< array name. length; i++) {
Array name of string type [i]= input.next ();
Array name of type int [i]= input.nextint ();
An array of type Double [i]=input.nextdouble ();
}
Five, array sorting problem
Use arrays. Sort (array name);
Implementing an element in an array output in ascending or descending order
Six, to find the maximum or minimum value
Implementation idea: The value of the 1th element in the array, assumed to be the maximum (small) value "assignment", and then each element in the array is compared with the imaginary maximum (small) number, if the value of an element in the array is larger (small) than the maximum (small) number, the value of the element is saved in the variable corresponding to the imaginary maximum (small) value "Assign a value to an imaginary value variable"
Seven, find the most value in the array position
1, outside the loop, define the variable K (assigned to 0), to save the most value of the subscript
2, in the cycle, the comparison to find the maximum value of the same time, the variable K assignment
Viii. Fibonacci Sequence
1,1,2,3,5,8,13,21,34 ... Find the value of the 20th number? (using loops)
Rule: First i + (i+1) = (i+2)
Step 1: Define an array with an array length of 20
int []nums=new int[20];
Step 2: Get the initial value of the No. 0 and 1th numbers according to the law
Nums[0]=1;
Nums[1]=1;
Step 3: Assign values to all elements in the array by looping
for (int i=0;i<nums.length-2;i++) {
Nums [i+2]=nums[i]+nums[i+1];
}
Or
for (int i=2;i<nums.length;i++) {
NUMS[I]=NUMS[I-2]+NUMS[I-1];
}
Step 4: Output NUMS[19] Value
Nine, (a) is known an array, how to insert a "new element" into the array at the specified position?
Implementation ideas:
1, define a length of "known quantity leader degree +1" of the new array yy
2. Using the 1th loop, save the value of each element in the known quantity group to the new array yy
3, using the 2nd loop, the element in the new array yy, starting from the bottom 2nd (the length of the new array yy-2) element, to the specified position, and then backward to move one position
4. Assign the "new element" directly to the specified element in the new array yy (subscript to the specified position)
5. Use the 3rd loop to output the value of the new array yy
(ii) a known array in ascending or descending order, inserting a new value into the array, and after the insertion succeeds, the array is still sorted in ascending or descending order?
1, set the insertion position variable index's initial value is: array length-1;
2. Use the 1th loop to make a comparison between the inserted value and each element in the array, and if the inserted value is larger or smaller than the value of an element in the arrays, assigns the underlying value of the element to the insertion position variable index and terminates the loop
3. Use 2nd loop, move position, from "Array length-2" to "Insert position variable index"
4. Save the inserted value to the position labeled index below the array
5, use the 3rd loop, output the value of all elements of the array, and output the position where the new value is inserted
--------------------------------------------------------------------------------
[DREAM1] type int, char
[Dream2]case structure can have multiple
[DREAM3] program jumps out of switch structure
[DREAM4] constant value cannot be the same
Same as else in [DREAM5] and if structures
Java (Eighth chapter)