6, Object array

target

  ✨ Brain storm topic: Fundamentals of Java Chapter 6 - (object array)

  ⏰ Recording time: 18:10, October 14, 2021

What is an object array

Object array, that is:

The object array stores the object name (address value)

Why use object arrays

If you create many student objects, how to store and manage these student objects can be solved with arrays. Such arrays are called object arrays

The type of object array declaration is class, as follows:

Student  [ ] students =new Student[5];

This is an array of Student type, which means that it can only store data (objects) of Student type. So arrays are called object arrays

//This is an array of String type. Only Strong type data can be placed
String [] arrs = new String[5];
//This is an array of int type, which can only be integer: 10, 20, 30
int [] arr2 = new int [5];
//This is an array of double type, which can only put decimals: 10.5, 20.0, 35.5
double [] arr3= new double[5];

Concept of object array

Any array can be divided into two types [static] and [dynamic] int[] arr = new int [9];

//It defines the length and does not assign a value to its contents, so its contents can be changed, so we call it (dynamic array)

int[] arr = new int[] { 1,2,3,4....}; // Define the content of an element. Its content is its length. Because its length and content have been fixed, we call it (static array)

java array objects are also divided into static and dynamic

Use of object arrays

It is used like an array, but the type it stores is (address value), that is, a space. The format is the same as that of an array

Precautions for object array:

If you create an object, that is, a new object, it has a corresponding address value, but if you define a space repeatedly in the loop, it will recreate a space (address value)

The original address value (space) will be recycled by the garbage collection mechanism! The newly created space will replace the previous space!

Steps:
    1.First, define a class and create nonparametric construction and parametric construction 
	2.Array object, can't directly traverse to get its content, so I need to use it Getter and Serter of Gerter Indirect acquisition


    public  class Subclass{

        // Nonparametric structure
        public Subclass (){
        }
// Structure with parameters, method 1
        public Subclass( String name, int age){   //Here are two different data types. You must create a structure with parameters to assign values of corresponding types
            this.name=name; //Assign the formal parameter element = of the constructor to the member variable
            this.age=age;    //Assign the formal parameter element = of the constructor to the member variable
        }
        //The array object cannot be traversed directly to get its contents, so I need Gerter of Getter and Serter
// Getter get, indirect use
        public  String  getName(){
            return  name;  //return
        }
        // Getter get, indirect use
        public  int  getAge(){
            return  age; //return
        }
    }
	1. Create implementation class
	2. Create an array of subclass objects
            3. Create instantiated objects, assign values, or directly new Class name (element content)



    // Create an array of subclass objects
    Subclass[]  arr = new Subclass[5];
    //Method 1: create an instantiated object to assign values
    Subclass   a1  =new Subclass ("diga", 19);
    arr[0]=a1;   //Assign the element content of a1 to the index 0 of the arr array
// Method 2: directly new class name (assign value by element content)
    arr[1]  = new Subclass ("Taylor", 12);
    arr[2]  = new  Subclass ("Saiwen", 15);
            // Traverses the element contents of the output array object
            for(int i=0; i<arr.length; i++){
        System.out.println(arr[i].getName()+","+arr[i].getAge()); //Array index. Get element contents
    }

Key understanding: any reference type can be replaced by [new address value]

// Basic steps:
     1.Create an array
      int[] arr  =new int[10];

    1.2 Use the same array name, Then create a new array
        arr =int[11];

  So actually: arr The space of the array becomes the space of 11,  10 Because there is no label pointing to,It will be recycled by the garbage collection mechanism! 
       however: 10 Spatial data,No room for 11, But you can use Arrays Method of tool class, copy() Put the spatial data of the previous array,Array copied to 11
           
           
           
      2.Create an object
          Demo01  obj  =new Demo01();

       2.1 Store some element values on this object
           obj.name='Reset';

      2.2 Create another object with the same object name
          That is, generating a space,Give the address value to the object name
          
          obj =new Demo01();
        
      //  Note: in this way, a space is reopened to give the address value to the corresponding array name or object name, but the previously created space will be recycled by the garbage collection mechanism because there is no label pointing to it!
          
            
           
        a key: stay     Overloading is also caused in the loop!
            
            for(int i=0; i<30; i++){
                Demo obj =new Demo();
                
            }
        // Each cycle will reallocate space for this object, but the template is the same. The content of the class is the template of the object!
       

Note: this can be done as long as it is of reference type!

If there are deficiencies, please give advice, {personal understanding}

Tags: Java

Posted on Thu, 14 Oct 2021 17:45:45 -0400 by andychamberlainuk