学java的第10天,面对对象最后一节,23.2.16

  1. 主程序

import java.util.Scanner;

/*

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

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

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

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

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

public class StudentInfoTest {

public static void main(String[] args) {

//创建数组

StudentInfo[] arr = new StudentInfo[3];

//添加一个学生对象

StudentInfo stu1 = new StudentInfo(1, "张三", 21);

StudentInfo stu2 = new StudentInfo(2, "李四", 21);

StudentInfo stu3 = new StudentInfo(3, "王二麻子", 21);

arr[0] = stu1;

arr[1] = stu2;

arr[2] = stu3;

//再添加一个学生对象,并在添加的时候进行学号的唯一性判断

//1.创建学生对象

StudentInfo stu4 = new StudentInfo(4, "王五", 20);

//2.唯一性判断

boolean flag=contains(arr,stu4.getId());

// 已存在----不添加

if(flag){

System.out.println("当前id已存在,请从新输入");

}else{

// 不存在----直接添加

//3.数组长度判断

int count =count(arr);

if (count==arr.length){

//①数组已存满----创建一个新数组,新数组长度=老数组长度+1

//把老数组中的元素拷贝到新数组

StudentInfo[] arr1 = new StudentInfo[arr.length+1];

arr1=copy(arr);

arr1[arr1.length-1]=stu4;

//输入id查询学生信息是否存在

Scanner sc=new Scanner(System.in);

System.out.println("请输入要删除的学生的学号:");

int id=sc.nextInt();

//如果存在

int count1=delete(arr1,id);

if(count1==arr1.length){

System.out.println("学生信息删除失败!");

}else{

arr1[count1]=null;

System.out.println("计数器:"+count1);

}

//遍历所有学生信息

traversal(arr1);

}else{

//②数组未存满----直接添加

//count获取的是当前数组中已有元素数量,也是下一个空位置的索引

arr[count]=stu4;

//遍历所有学生信息

traversal(arr);

//输入id查询学生信息是否存在

Scanner sc=new Scanner(System.in);

System.out.println("请输入要删除的学生的学号:");

int id=sc.nextInt();

//如果存在

int count1=delete(arr,id);

if(count1==arr.length){

System.out.println("学生信息删除失败!");

}else{

arr[count1]=null;

System.out.println("计数器:"+count1);

}

//遍历所有学生信息

traversal(arr);

}

}

}

//===================================================

// 1.唯一性判断

//要返回存在/不存在

public static boolean contains (StudentInfo[]arr,int id){

for (int i = 0; i < arr.length; i++) {

boolean flag=false;

StudentInfo stu=arr[i];

int sid=stu.getId();

if(sid==id){

return true;

}

}

return false;

}

//===================================================

//判断在数组中有几个元素

public static int count(StudentInfo[]arr){

int count=0;

for (int i = 0; i < arr.length; i++) {

if(arr[i]!=null){

count ++;

}

}

return count;

}

//===================================================

//将老数组的数据拷贝到新数组中

public static StudentInfo[] copy(StudentInfo[]arr) {

StudentInfo []arr1=new StudentInfo[arr.length+1];

for (int i = 0; i < arr.length; i++) {

arr1[i]=arr[i];

}

return arr1;

}

//===================================================

//遍历数组中的所有元素

public static void traversal(StudentInfo[]arr){

for (int i = 0; i < arr.length; i++) {

if (arr[i]!=null) {

StudentInfo s = arr[i];

System.out.println(s.getId() + " " + s.getName() + " " + s.getAge());

}

}

}

//===================================================

//判断学生信息是否存在

public static int delete(StudentInfo[]arr,int id){

int count =0;

for (int i = 0; i < arr.length; i++) {

if (id==arr[i].getId()){

return count;

}else{

count ++;

}

}

return count;

}

//===================================================

//===================================================

}

  1. javaBean

public class StudentInfo {

//成员变量

int Id;

String name;

int age;

public StudentInfo() {

}

public StudentInfo(int Id, String name, int age) {

this.Id = Id;

this.name = name;

this.age = age;

}

public int getId() {

return Id;

}

public void setId(int Id) {

this.Id = Id;

}

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值