Object array quick dialing java 0913

Object array quick dialing java 0913

video

https://www.ixigua.com/6871892334255014413?logTag=r6u2cq3d6rgheoFkHT3y_

What is an object array

Let me talk about what is an array

Array is an ordered data container

Save data of basic data types

What is an object array

Object arrays are also data containers

Some user-defined objects are saved

How to define an array of objects

How to define ordinary array?

Define an integer array with a capacity of 5 and the encoding is as follows

        int[] numArray = new int[5];

Format summary

类型名[] 变量名 = new 类型名[长度];

The object array definition format is the same as above

类名称[] 变量名 = new 类名称[长度];

So you can define an array of specified length

Specially store objects (objects must be instantiated by this class)

Create an array of objects

        // 新一个对象数组 长度为4,可以保存四个学生对象
        Student[] stuArray = new Student[4];

The array is created, which is equivalent to this effect

Insert picture description here

null is the default value corresponding to each subscript of the object array

Add members to the space of the object array

数组[下标] = 对象;

Code

        // 新一个对象数组 长度为4,可以保存四个学生对象
        Student[] stuArray = new Student[4];
        // 0号位置放张三
        stuArray[0] = zs;
        // 1号位置放李四
        stuArray[1] = ls;
        // 2号位置放王五
        stuArray[2] = ww;

effect

Insert picture description here

Traversal of object array

Once the traversal has obtained the value corresponding to each subscript in the array

This value is one object after another

Is the object we put in

This object has its own attributes, such as name and number

because

The class of the object of the current example is defined like this

public class Student {
    
    
    // 学号
    int number;
    // 姓名
    String name;
}

and so

        // 遍历对象数组
        for (int i = 0; i < stuArray.length; i++) {
    
    
            // for拿到了下标,通过  数组【下标】可以获取数组下标对应的值
            // 数组下标对应的值是什么? 是一个一个的对象
            // 所以 数组【下标】 就是对象。  然而,对象是具有属性的。
            // 所以  数组【下标】.属性 ,是可以访问到对象的值的

            System.out.println("遍历得到的对象的打印:" + stuArray[i]);
            // 如果下标对应的值不是null,那就是对象。 如果是null就跳过
            if (stuArray[i] == null) {
    
    
                System.out.println("不是对象,没有name与number属性");
                continue;
            }
            System.out.println("该对象的name属性值是:" + stuArray[i].name);
            System.out.println("该对象的number属性值是:" + stuArray[i].number);

result

遍历得到的对象的打印:Student@591c5342
该对象的name属性值是:张三
该对象的number属性值是:1
遍历得到的对象的打印:Student@529e3fc2
该对象的name属性值是:李四
该对象的number属性值是:2
遍历得到的对象的打印:Student@136c03ee
该对象的name属性值是:王五
该对象的number属性值是:3
遍历得到的对象的打印:null
不是对象,没有name与number属性

summary

Object array

Is a container for objects

and so

Object array [subscript] If it is not null, it is an object

Object properties and methods can be called

able to pass

Object array [subscript]. Property gets the property value of the specified subscript

Object array [subscript]. Method () Call a method of the object

Guess you like

Origin blog.csdn.net/ifubing/article/details/108564137