当前位置: 首页 > news >正文

练习:对象数组 5

定义一个长度为 3 的数组,数组存储 1~3 名学生对象作为初始数据,学生对象的学号,姓名各不相同。学生的属性:学号,姓名,年龄。

要求 1:再次添加一个学生对象,并在添加的时候进行学号的唯一性判断。

要求 2:添加完毕之后,遍历所有学生信息。

要求 3:通过 id 删除学生信息,如果存在,则删除,如果不存在,则提示删除失败。

要求 4:删除完毕之后,遍历所有学生信息。

要求 5:查询数组 id 为“2022072002”的学生,如果存在,则将他的年龄+1 岁。

代码一:

//对象数组 5:
//代码一:
package demo03;
public class Students {//定义学生的三个私有属性:姓名(name)、学号(id)、年龄(age):private String name;private int id;private int age;//空参构造:public Students() {}//含参构造:public Students(String name, int id, int age) {this.name = name;this.id = id;this.age = age;}//get 和 set 方法:public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}

代码二:

//对象数组 5:
//代码二:
package demo03;
public class StudentsTest {public static void main(String[] args) {//定义一个长度为 3 的数组,用于存储学生对象;Students[] stu = new Students[3];//存储 3 名学生对象作为初始数据:Students stu1 = new Students("张三", 2022072001, 20);Students stu2 = new Students("李四", 2022072002, 21);Students stu3 = new Students("王五", 2022072003, 22);stu[0] = stu1;stu[1] = stu2;stu[2] = stu3;//再次添加一个学生对象:Students stu4 = new Students("赵六", 2022072004, 23);//判断学号唯一性:boolean result = idJudge(stu, stu4);if(result) {System.out.println("学号已经存在!");}else {int count = countNumber(stu);if(count == stu.length) {Students[] newArr = creatNewArr(stu);newArr[count] = stu4;printArr(newArr);}else {stu[count] = stu4;printArr(stu);}}//删除 id:int index = index(stu, 2022072001);if(index >= 0) {stu[index] = null;System.out.println("删除 id 后的学生信息:");printArr(stu);}else {System.out.println("id 不存在,删除失败!");}//查询数组 id 为“2022072002”的学生的索引:int studentIndex = index(stu, 2022072002);//如果存在则将年龄 +1:if(studentIndex >= 0) {int age = stu[studentIndex].getAge() + 1;stu[studentIndex].setAge(age);System.out.println("修改年龄后的学生信息:");printArr(stu);}else {System.out.println("id不存在,修改失败!");}}//定义学号唯一性判断的方法:public static boolean idJudge(Students[] stu, Students stu4) {for(int i = 0; i < stu.length; i++) {if(stu[i] != null) {if(stu[i].getId() == stu4.getId()) {return true;}}}return false;}//定义一个方法来判断数组中存了几个元素:public static int countNumber(Students[] stu) {int count = 0;for(int i = 0; i < stu.length; i++) {if(stu[i] != null) {count++;}}return count;}//定义一个创建新数组的方法:public static Students[] creatNewArr(Students[] stu) {Students[] newArr = new Students[stu.length + 1];for(int i = 0; i < stu.length; i++) {newArr[i] = stu[i];}return newArr;}//定义一个方法来遍历数组中每个元素:public static void printArr(Students[] stu) {for(int i = 0; i < stu.length; i++) {if(stu[i] != null) {System.out.println(stu[i].getName() + "------" + stu[i].getId() + "------" + stu[i].getAge());}}}//定义一个方法用于判断 id 所在索引位置:public static int index(Students[] stu, int id) {for(int i = 0; i < stu.length; i++) {if(stu[i] != null) {if(stu[i].getId() == id) {return i;}}}return -1;}
}

运行结果:

http://www.dtcms.com/a/271634.html

相关文章:

  • 【隐藏谷歌原生Qsb,将Widget中的Qsb组件替换至原位(解决GMS包添加后默认Qsb搜索框无法操作的问题)】
  • QT事件处理机制详解:从原理到实战
  • 深入浅出二分法:从实际问题看“最小化最大值”问题的求解之道
  • 技术支持丨解决 ServBay 在 Windows 启动时反复提示安装 .NET 的问题
  • 数据治理全景能力图谱与路线图:构建企业级数据治理的全貌视角
  • React 19 概览:新特性与生态系统变革
  • 缺乏项目进度数据沉淀,如何做好进度复盘
  • linux-用户和组
  • GIS使用方法详解
  • 在线生成树形目录文本
  • uniapp真机调试“没有检测到设备,请插入设备或启动模拟器后点击刷新再试”
  • TCP/IP常用协议
  • sftGRPO
  • 链表算法之【删除链表的倒数第n个节点】
  • 如何将FPGA设计的验证效率提升1000倍以上(3)
  • Spark流水线数据对比组件
  • vue3实战:.ts文件中的interface定义与抛出、其他文件的调用方式
  • Vue 中使用 Cesium 实现可拖拽点标记及坐标实时显示功能
  • 投机采样(Speculative Decoding)
  • Python—数据容器
  • 【解决方法】ollama在powershell或者cmd运行时乱码报错
  • C++11 std::move与std::move_backward深度解析
  • 7、整合前几篇插件列表
  • 单片机STM32F103:DMA的原理以及应用
  • 滚筒式茶叶杀青机设计【12张+总装图】+三维图+设计说明书+绛重
  • Hugging Face Agents Course unit1笔记
  • Pycharm 报错 Environment location directory is not empty 如何解决
  • Vue2开发:使用vuedraggable实现菜单栏拖拽
  • 什么是AI Agent同步调用工具和异步调用工具?
  • python实践思路(草拟计划+方法)