集合基础--对象数组

1.题目

复制代码
需求:将(张三,23)(李四,24)(王五,25)封装为3个学生对象并存入数组
随后遍历数组,将学生信息输出在控制台

2.思路

复制代码
1.定义学生类准备用于封装数据
2.动态初始化长度为3的数组,类型为Student类型
3.根据需求创建3个学生对象
4.将学生对象存入数组
5.遍历数组,取出每一个学生对象
6.调用对象的getxxx方法获取学生信息,并输出在控制台

3.代码

错误代码 展示

java 复制代码
import domain.Student;

public class 对象数组 {
//    思路:
//    1.定义学生类准备用于封装数据
//    2.动态初始化长度为3的数组,类型为Student类型
//    3.根据需求创建3个学生对象
//    4.将学生对象存入数组
//    5.遍历数组,取出每一个学生对象
//    6.调用对象的getxxx方法获取学生信息,并输出在控制台
    public static void main(String[] args) {
        //2.动态初始化长度为3的数组,类型为Student类型
        Student[] arr=new Student[3];
        //3.根据需求创建3个学生对象
        Student stu1=new Student("张三",23);
        Student stu2=new Student("李四",24);
        Student stu3=new Student("王五",25);
        //4.将学生对象存入数组
        arr[0]=stu1;
        arr[1]=stu2;
        arr[2]=stu3;
        //5.遍历数组,取出每一个学生对象
//        for (int i = 0; i < arr.length; i++) {
//            Student temp=arr[i];
//            System.out.println(temp.getName()+"..."+temp.getAge());
//        }
        for (int i = 0; i < arr.length; i++){
            System.out.println(arr[i]);
        }
    }
}

错误点出现在 最后的for循环处,直接打印 得到的是存储的地址:

正确代码

java 复制代码
import domain.Student;

public class 对象数组 {
//    思路:
//    1.定义学生类准备用于封装数据
//    2.动态初始化长度为3的数组,类型为Student类型
//    3.根据需求创建3个学生对象
//    4.将学生对象存入数组
//    5.遍历数组,取出每一个学生对象
//    6.调用对象的getxxx方法获取学生信息,并输出在控制台
    public static void main(String[] args) {
        //2.动态初始化长度为3的数组,类型为Student类型
        Student[] arr=new Student[3];
        //3.根据需求创建3个学生对象
        Student stu1=new Student("张三",23);
        Student stu2=new Student("李四",24);
        Student stu3=new Student("王五",25);
        //4.将学生对象存入数组
        arr[0]=stu1;
        arr[1]=stu2;
        arr[2]=stu3;
        //5.遍历数组,取出每一个学生对象
        for (int i = 0; i < arr.length; i++) {
            Student temp=arr[i];
            System.out.println(temp.getName()+"..."+temp.getAge());
        }
//h
    }
}

定义一个Student类型的变量temp来存储数组数据

4.注意点:

新建一个domain包,包下新建Student类用于封装数据

java 复制代码
package domain;

public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

定义好 name和age后,右键"生成"

以上操作可以生成构造函数、get()方法、set方法

5.缺点

当有新的信息想要插入,该对象数组是容量不够的。只能新建容量更大的数组才能让新的信息插入。

相关推荐
CoovallyAIHub2 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP3 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo3 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo3 小时前
300:最长递增子序列
算法
CoovallyAIHub8 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub8 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js