the concept of arrays
Array one data type-"reference type"
Role
Previously learned data types can only be loaded with one value, and arrays may be loaded with multiple values.
One-dimensional array definition reference
Syntax 1: type of array element [] variable name; (Java mode-recommended)
Syntax 2: Type variable name for array element []; (c reserved)
Create an Array object--Define the reference and initialize it statically: Know the length and content of the array.
Syntax: type of array element [] variable name =new array element type []{element 1, Element 2 ...};
Create an Array object--Define the reference and initialize it dynamically: Know the length of the array.
Syntax: type of array element [] variable name =new array element type [length];
Note: After dynamic initialization, the array elements have default values, and the rules are consistent with the properties.
Access and traversal of one-dimensional arrays
1) Length of array: array reference. length;
2) a) Access the value of the array location:
array element type variable = array reference [subscript];
b) Set the value of the array position:
Array reference [subscript]= The value of the new element;
3) Traversal: Find all the elements of the array
for (int subscript = 0, subscript <=len-1; subscript + +) {
Current element
array element type variable = array reference [subscript];
}
The attention point of the array
1) Array of elements binding the subscript, subscript range 0 to Len-1
2) When accessing an array element, the subscript must be (0 to len-1) otherwise-"error arrayindexoutboundsofexception
3) The elements of the array must be of uniform type (type compatible)
A) Basic type meets small type auto-turn large type
b) The reference type satisfies the subtype auto-turn-over type
4) array underlying storage element memory location is contiguous, access element is high efficiency (read and set)
Array-dependent algorithms
Bubble sort Logic
1) Sort each trip to determine the location of a number
2) Total n (number of numbers)-1 times
3) The location of each trip to determine the number-"by two number (front and back of the number) comparison n-i (first several) times
4) Two numbers to compare, ascending-"put two numbers in the back of the large-" descending-"small put back
Finding logic by dichotomy
1) Ascending sort
2) Compare the values of the intermediate positions of the current range (start and end positions) to the target value each time the element is found
3) Initial time: Start position 0, end position is len-1
4) Compare the value of the middle position to the target value-"Each time you find-"
A) middle value: End position set to middle value-1
b) Middle value small: Start position set to middle value +1
c) Equality: found, return
Use of two-dimensional arrays
Understanding: Imagine a two-dimensional array as a table
Length of the two-dimensional array--the length of the line
Length of a one-dimensional array of two-dimensional arrays-the length of the column
Static initialization-Define references and create objects
Grammar:
Elements of a one-dimensional array [] [] Two-dimensional array names =new elements of a one-dimensional array [][]{
{One-dimensional array element 1, one-dimensional array element 2...},//one-dimensional array-first row
{...},//one-dimensional array-second row
{...},//...
};
Dynamic initialization
Syntax 1:
Elements of a one-dimensional array [] [] Two-dimensional array names =new elements of a one-dimensional array [president degree] [];
Elements of a two-dimensional array--"initialized to null
Syntax 2:
Elements of a one-dimensional array [] [] Two-dimensional array names =new elements of a one-dimensional array [the length of the president] [column];
The element of a two-dimensional array--"Initializes a one-dimensional array of column lengths.
Access and traversal of two-dimensional arrays
1) length of two-dimensional array: two-dimensional array reference. length;
2) Elements of a two-dimensional array (one-dimensional array): two-dimensional array reference [two-dimensional array subscript];
3) The length of the elements of a two-dimensional array (one-dimensional array): two-dimensional array reference [two-dimensional array subscript].length;
4) Elements of a two-dimensional array (one-dimensional array): two-dimensional array reference [two-dimensional array subscript] [one-dimensional array subscript];
5) Traversal
Traversing two-dimensional arrays-elements (one-dimensional arrays)
for (int two-dimensional subscript = 0; two-dimensional subscript <=len-1; two-dimensional subscript + +) {
Current element-"one-dimensional array"
One-dimensional array element type [] variable = two-dimensional array reference [subscript];
Traversing one-dimensional arrays-elements (specific data)
for (int one-dimensional subscript = 0, one-dimensional subscript <=len-1; one-dimensional subscript + +) {
Current element-specific data
One-dimensional array element type variable = one-dimensional array reference [one-dimensional subscript];
}
}
Java Beginner Notes Summary Day10