1. Functions and types of arrays
1.1 Effects
A set of data that stores the same data type
Centralized storage management for the same type of data, easy traversal
1.2 Data types
The type of the array is the type of data stored in the array
Points:
All elements in an array must belong to the same data type
All elements in an array are stored continuously in memory
Characteristics:
The array itself is also an application type
The array name is simply a reference to the array object created in the heap, which holds a set of other references or direct values
2. Declaration, creation of an array
2.1. Declaration of an array
Tell the system what the data type is
Syntax: data type [] array name;
2.2. Creation of arrays
Allocating memory space to an array (new operator)
2.3. Declaring and creating an array
data type [] Array name = new data type [array size]
Example: int [] num = new int [5];
When you create an array, be sure to determine the size of the array.
Array features
The array has a property of length, which indicates how many elements that this array object can store, that is, the length of the array, length is not the actual number of elements
The "[]" bracket is the only way to access the array formation members, for example: Num[0];0 represents the subscript of an array,
An array of objects holds a reference, and the primitive type array directly holds the value of the base type
The newly generated array object, where all references are automatically initialized to null, and the base type array members have their own default values (numeric type is automatically initialized to 0, the character type is 0, and the Boolean type is false).
3. Initializing an array
3.1. Initialization of arrays
Assign a value to a member in an array
3.2 Two ways to assign a value to an array
Method 1: Edge Declaration Edge Assignment (static initialization)
int [] score = {A-i};
int [] score= new int [] {a-i};
The elements are separated from the elements by commas;
Method 2: Dynamically obtain (from keyboard input) information and assign values
Scanner sc=new Scanner (system.in);
for (int i=0;i<3;i++) {
System.out.println ("Please enter");
Score[i]=sc.next ();
}
4. Commonly used array sorting algorithms
4.1 Bubble Sort Method
How it works: It's a simple sort algorithm that repeatedly visits the sequence to sort, compares two elements at a time, and swaps them if they're in the wrong order. Repeat until no more swapping is needed, knowing that the sequence is sorted.
Cases:
Console: 9,8,7,6,5,4,3,2,1,0
4.2 Select Sort Method
How it works: first find the smallest element in an unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest element from the remaining unsorted elements, and then place it at the end of the sort sequence. And so on until all elements are sorted.
Console: 0,1,2,3,4,5,6,7,8,9
4.3 Insert Sort method
How it works: it is done by constructing an ordered sequence, for unsorted data, from a backward forward scan in a sorted sequence, to find the appropriate location and insert. From the back-forward scanning process, the ordered elements need to be moved backwards and forwards, providing the insertion space for the newest elements.
Console: 0,1,2,3,4,5,6,7,8,9
5. Declaration, creation of two-D arrays
A two-dimensional array can be seen as an array of elements
The Declaration and initialization of two-dimensional arrays in Java should be carried out in order from high-to low-dimensional
Example: int [] [] arr1=new int [3][];
int [] [] arr2 =new int [3][5];
Although the two arrays are created differently, the system allocates the same amount of heap memory space for them
For a two-dimensional array of any data type, the size of the first dimension determines the size of the two-dimensional array object because the member of the two-dimensional array is an array reference, and the array reference itself is of a fixed size
5.1. Initializing a two-dimensional array
Example: int [] [] arr={{1,2,3},{4,5},{2,3,5}};
The two-dimensional array, arr, contains 3 low-dimensional arrays, so it has a high dimension of 3;
Take value: Example: arr[0][0]=1;
arr[1][1]=5;
Java Basics (5) array