Review Day 2 Part 2 Arrays

An overview of arrays

Array: Combination of data (a set of data) -> container​A 
variable
is also a container, but a variable can only store one data at a time; if there is a batch of data of the same type, it can be stored in an array;​Knowledge 
of 
the container 
    1. How to create a container 
    2. Master the four functions of adding, deleting, modifying and checking the container (CRUD) 
    3. Traversing the container -> Traversing: Take out each element in the container from the container in turn

Features of arrays

1. Arrays are one of the Java reference data types! 
    Whenever an array is created, an object of array type is generated!! 
2. Once the array is defined, the length cannot be changed! If the length of the array changes, Then it must be to create a new array! -> Disadvantages! 
    //The later array is replaced by the collection (the length of the collection is variable), but the underlying layer of the collection is actually an array! 
3. The elements in an array must be of the same data type; //Convenient management, from the perspective of an array 
4. Elements of basic data types and elements of reference data types can be stored in the array; -> Benefits! The array can store anything! 
        //Macro perspective: the entire array concept! 
5 . Each element in the array has a number: professionally called index-> ​​In order to facilitate the query element     
    index starts from 0, the maximum index number in an array: length-1 
6. The concept of array has dimension 
    One dimension: line segment : A line segment is composed of points. One- 
        dimensional array: The elements that make up a one-dimensional array can be understood as points that make up a line segment. 
    Two-dimensional: Surface: A surface is composed of line segments. 
        Two-dimensional array: A one-dimensional array is stored in a one-dimensional array // No need to spend energy to study two-dimensional arrays 
        
Example: 
    one-dimensional array: a pack of cigarettes 
    two-dimensional array: one cigarette

Array initialization

Dynamic initialization of arrays 
    ** data type [] array name = new data type [length]; 
format
explanation: 
    data type: the data type of elements in the array 
    []: one-dimensional array 
    array name: the same function as the variable name, use The array is using the name of the array -> naming method: small hump 
    = : the assignment operator assigns the address of the array body on the right side of the equal sign to the array name on the left 
    new: create an object (an array is also an object) / open up space in memory (with space will have address value) 
    data type: the data type of the elements in the array -> the type before and after the requirement must be consistent! 
    Length: the number of elements in the array, the capacity of the array!     
    ​The full format 
of the static initialization of 
the array : data type[ ] Array name = new data type [] {element 1, element 2, element 3,...}; 
    **simplified format: data type [] array name = {element 1, element 2, element 3,. ....}; 
​Format
explanation: 
    {element 1, element 2, element 3,...} : Static initialization will list the elements in the array one by one! 
The difference between dynamic and static initialization arrays: 
    dynamic initialization data: by The programmer determines the length of the array/the number of elements, and the computer determines the default value (initial value) of each element according to the data type of the array element.
         
        Integer type (byte, short, int, long): 0
        Floating point type (float, double): 0.0 
        Character type (char): null character -> '\u0000' 
        Boolean type (boolean): false 
        All reference data type arrays: null 
    Static initialization array: each array is determined by the programmer The specific value of the element, the length of the array/the number of elements is automatically calculated by the computer; 
​Usage
scenario: 
    dynamic: the programmer only knows the length of the container, but not the specific value of each element in the container, choose dynamic static 
    : programmer Clear the specific value of each element in the automatic container, choose 
    
    
the difference between the static reference data type object and the basic data type variable: 
    when outputting the basic data type variable: directly see the specific value 
    when outputting the reference data type object: see is the address value of the object in memory

Array addition, deletion, modification and query four types of functions

There are four types of functions for adding, deleting, modifying and checking arrays: 
    Add: Once the length of the array is defined, it cannot be changed, and there is no function of adding! Delete: 
    Once the length of the array is defined, it cannot be changed, and there is no function of deleting!   
    Modify: Check first and then modify 
        **Array name[index] = the value to be modified; 
    check: //use the index of the element in the array to find the element 
        **array name[index]: query the value of the element at the specified index position in the array (check: copy) 
        // The array has a length attribute     
        **array name.length: query the length of the specified array/number of elements    

Array traversal standard code

//Shortcut code: Array name.fori Traverse the array name in forward order.forr Traversal in reverse order 
        for (int i = 0; i < arr1.length; i++) { 
            System.out.println("arr1["+i+"] = " + arr1[i]); 
        }''''
            

Java memory allocation

Stack memory: stack 
    function 1: provide memory space for method execution in the program 
    function 2: store local variables//local: inside the method 
    function 3: stack memory can represent the execution process of the program -> methods are pushed into the stack, and methods are popped out of the stack 
    
    memory Features: 
        1. Method push-in and out-stack: first-in last-out -> gun magazine 
        2. StackOverFLowError pops up immediately after the method in the stack memory is used 
        
    : stack memory overflow error     
        //It can only reduce 
the number 
Heap memory: memory -> memory 
    Function 1: store new things -> the body of the object is in the heap 
    Function 2: After JDK1.7, the constant pool is stored in the heap memory 
    
    Features of the heap memory: 
        1. As long as it is in the heap memory Open up space (create an object) in the heap memory, and the heap memory will assign an address value to this space (object 
        ) 
                    . ,long) : 0 
                    Floating point type (float,double): 0.0 
                    Character type (char): Null character -> '\u0000'
                    Boolean type (boolean): false 
    Native method: The method for interacting with the operating system (the native method will be modified by the native keyword in Java)
                    All reference data type arrays: null 
        3. After the objects in the heap memory are used up, they are not recycled (dead) immediately, but wait for the gc (garbage collection mechanism) to recycle when it is free.     
            //Can actively start gc: System.gc( ) -> API 
            
      OutOfMemoryError: heap memory overflow error 
        //can only reduce the number of object creation 
method
area: 
    store 1. bytecode object (compile (bytecode file.class file) first, then run) 
        // a class After the file is executed, it is only possible to generate a bytecode object --> object type verification 
        // The overview (directory) of the class is encapsulated in the bytecode object: to reflect which members (member variables and member methods) are in a class ) 
        member: Indicates a positional relationship -> stored outside the method in the class 
    2. Constant pool: memory container for storing constants    
        //The constant pool has been moved to the heap memory since JDK1.7 
        
---------- -------------------------------------------------- ----         
Local method area: the area where the local method is executed 
Register: the block related to the operation of the CPU

Memory map for initialization of two arrays

Two references point to the same memory address: any one reference modifies the memory area, and the other reference can also see the change of memory data!! ​The reference 
of the 
operation

2 small questions about array operations

    ArrayIndexOutOfBoundsException: Array index out of range exception -> index out of bounds exception//can only appear in the array! 
        Cause: access to an array index number that does not exist 
        Solution: 
            1. Do not visit 
            2. Please use the correct index number 
            
    NullPointerException: Null pointer exception//This problem may occur as long as the object is used. 
        Reasons: 
            1. Use an object reference with a value of null to access the internal space 
            2. Use an object reference with a value of null to access the properties of the object 
            3 .Use an object reference with a value of null to call the method of the object 
            //Do not use an object reference with a value of null!! 
       Solution: 
            1. Do not use an object with a value of null 
            2. Find the used object, look for this When the object is assigned a value of null, modify the code here to give the object a correct address value      

Guess you like

Origin blog.csdn.net/m0_70793154/article/details/127166122