The concept of arrays
An array is a container that stores data of a fixed length, ensuring that multiple data types are consistent.
Ii. Array Definition (three ways)
[] array name = new array type [length];
For example: int[] arr = new int[8];
2. Data type [] array name = new data type []{element 1, element 2, element 3… };
For example: int[] arr = new int[]{1,2,3};
[] array name = {element 1, element 2, element 3… };
For example: int[] arr = {1,2,3};
Note: The length of the array, once specified, cannot be changed.
Array access
1. Index: Each element stored in the array is automatically assigned a number starting from 0. This automatic number is called the index of the array.
2. The format
Array name [index]
3. Array length property: each array has a length, and is fixed, Java assigns an array property to access the length of the array, statement: array name. Length, the result of the property is the length of the array, int type result, array maximum index value is: array name.
4. Index access to array elements:
Array name [index]= value, assigning values to the elements in the array;
Variable = array name [index], get the elements of the array;
4. Array principle memory map
Memory concept: Memory is an important component in the computer, temporary storage area, the function is to run the program, we write the program is placed in the hard disk, the program in the hard disk will not run, must be put into the memory to run, after running will empty the memory.
Memory partitioning for the JVM
1. Storage of arrays in memory
Common operations on arrays
1. Array out-of-bounds exception2. The null pointer is abnormal
Note: out-of-bounds exceptions for arrays are not allowed in development, and when they do occur, we have to change the code we wrote.
3. Array traversal
Array traversal: To get each element of the array separately, that is, traversal. Traversal is also the cornerstone of array manipulation.
4. Array retrieves the maximum element
5. Array inversion implementation idea: the farthest end of the array elements swap positions.
Arrays as methods return parameters and return values
1. The array is passed as a method parameter, which is the address of the array’s memory.
2. Array as the return value of the method, returns the memory address of the array.
3. Difference between method parameter types: When a method parameter is a basic data type, data value is passed; when a method parameter is a reference type, address value is passed. In short, the value does not change when the parameter is a primitive type; The address value does not change when it is a reference data type.