首页 > 其他分享 >面向对象:添加删除和修改学生信息

面向对象:添加删除和修改学生信息

时间:2023-03-19 10:44:08浏览次数:38  
标签:arr 删除 stu int id 面向对象 添加 Student public

package com.itheima.test7;

public class Test {
    public static void main(String[] args) {
        //定义一个长度为三的数组
        Student[] arr= new Student[3];
        //添加数据
        Student s1 = new Student(1,"lisa",25);
        Student s2 = new Student(2,"jisoo",26);
        //Student s3 = new Student(3,"rose",25);
        //赋值
        arr[0] = s1;
        arr[1] = s2;
        //arr[2] = s3;
        //1.添加一个对象
        Student s4 = new Student(4,"jennie",25);

        //唯一性判断,已存在,则不用添加
        boolean flag = contains(arr,s4.getId());
        if(flag){
            System.out.println("id重复");
        }else{
            //数组已满,创建新数组,新数组长度=老数组+1;
            //数组没满,直接添加    写成方法
            int count = getCount(arr);
            if(count== arr.length){
                //已经存满,将老数组传递给新数组
                Student[] newArr = createNewArr(arr);
                //count是填充新元素的索引
                newArr[count] = s4;
                //2.遍历所有学生信息
                printArr(newArr);
            }else{
                arr[count] = s4;
                printArr(arr);
            }

        }
    }

    //打印元素,需要新数组老数组都打印所以要写方法,只需要打印,返回值为空
    public static void printArr(Student[] arr){
        for (int i = 0; i < arr.length ; i++) {
            //数组要用变量接收
            Student stu = arr[i];
            if(stu!=null){
                System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName());
            }
        }
    }
    //创建新数组,返回的是Student[]类型的数组
    public static Student[] createNewArr(Student[] arr){
        Student[] newArr =new Student[arr.length+1];
        //循环遍历得到老数组中每一个元素
        for (int i = 0; i < arr.length; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    //定义一个方法判断数组中存了几个元素
    public static int getCount(Student[] arr){
        //统计不为空的元素
        int count= 0 ;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }

    //需要数组、id,需要判断是否存在,所以要返回,用boolean
    public static boolean contains(Student[] arr,int id){
        for (int i = 0; i < arr.length; i++) {
            //获取数组里的每一个学生对象
            Student stu = arr[i];
            if(stu != null){
                //null不能直接使用
                //获取数组中学生对象的id
                int sid = stu.getId();
                //比较
                if (sid == id){
                    return true;
                }
            }
        }
        //循环结束后未找到直接判断错误
        return false;
    }

}
 1 package com.itheima.test7;
 2 
 3 public class Test3 {
 4     public static void main(String[] args) {
 5         //定义一个长度为三的数组
 6         Student[] arr= new Student[3];
 7         //添加数据
 8         Student s1 = new Student(1,"lisa",25);
 9         Student s2 = new Student(2,"jisoo",26);
10         Student s3 = new Student(3,"rose",25);
11         //赋值
12         arr[0] = s1;
13         arr[1] = s2;
14         arr[2] = s3;
15         //3.通过id删除信息
16         //如果存在,则删除,如果不存在,则提示删除失败
17         int index = getIndex(arr, 4);
18         if(index>=0){
19             //如果存在则删除(索引置空)
20             arr[index] = null;
21             //遍历数组
22             printArr(arr);
23         }else{
24             System.out.println("id不存在,删除失败");
25         }
26     }
27 
28     //判断是否存在,所以必须返回
29     public static int getIndex(Student[] arr,int id){
30         for (int i = 0; i < arr.length; i++) {
31             //依次获得每一个学生对象
32             Student stu = arr[i];
33             //对stu进行一个非空判断
34             if(stu!=null){
35                 int sid = stu.getId();
36                 if(sid == id){
37                     return i;
38                 }
39             }
40 
41         }
42         //循环结束后还没找到
43         return -1;
44     }
45     //打印元素,需要新数组老数组都打印所以要写方法,只需要打印,返回值为空
46     public static void printArr(Student[] arr){
47         for (int i = 0; i < arr.length ; i++) {
48             //数组要用变量接收
49             Student stu = arr[i];
50             if(stu!=null){
51                 System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName());
52             }
53         }
54     }
55 }
 1 package com.itheima.test7;
 2 
 3 public class Test4 {
 4     public static void main(String[] args) {
 5 
 6         Student[] arr= new Student[3];
 7 
 8         Student s1 = new Student(1,"lisa",25);
 9         Student s2 = new Student(2,"jisoo",26);
10         Student s3 = new Student(3,"rose",25);
11 
12         arr[0] = s1;
13         arr[1] = s2;
14         arr[2] = s3;
15 
16         //id为2的年龄+1
17         int index =getIndex(arr,2);
18 
19         //判断索引
20         if(index>=0){
21             //存在年龄+1
22             Student s = arr[index];
23             //把原来的年龄拿出来
24             int newAge = s.getAge() + 1;
25             //把+1之后的年龄塞回去
26             s.setAge(newAge);
27             printArr(arr);
28         }else {
29             System.out.println("不存在");
30         }
31     }
32     //判断是否存在,所以必须返回
33     public static int getIndex(Student[] arr,int id) {
34         for (int i = 0; i < arr.length; i++) {
35             //依次获得每一个学生对象
36             Student stu = arr[i];
37             //对stu进行一个非空判断
38             if (stu != null) {
39                 int sid = stu.getId();
40                 if (sid == id) {
41                     return i;
42                 }
43             }
44         }
45         //循环结束后还没找到
46         return -1;
47     }
48 
49     //打印元素,需要新数组老数组都打印所以要写方法,只需要打印,返回值为空
50     public static void printArr(Student[] arr){
51         for (int i = 0; i < arr.length ; i++) {
52             //数组要用变量接收
53             Student stu = arr[i];
54             if(stu!=null){
55                 System.out.println(stu.getId()+","+ stu.getAge()+","+stu.getName());
56             }
57         }
58     }
59 }
 1 package com.itheima.test7;
 2 
 3 public class Student {
 4     private int id; //对字符串操作 先用int
 5     private String name;
 6     private int age;
 7 
 8     public Student() {
 9     }
10 
11     public Student(int id, String name, int age) {
12         this.id = id;
13         this.name = name;
14         this.age = age;
15     }
16 
17     public int getId() {
18         return id;
19     }
20 
21     public void setId(int id) {
22         this.id = id;
23     }
24 
25     public String getName() {
26         return name;
27     }
28 
29     public void setName(String name) {
30         this.name = name;
31     }
32 
33     public int getAge() {
34         return age;
35     }
36 
37     public void setAge(int age) {
38         this.age = age;
39     }
40 }

 

标签:arr,删除,stu,int,id,面向对象,添加,Student,public
From: https://www.cnblogs.com/Wang0626/p/17232582.html

相关文章

  • 2023年春面向对象第一单元
    23年春面向对象第一单元分析与总结目录 前言 架构  解析方法  数据结构  类图分析 基于度量的程序结构分析 BUG分析 互测相关 总结前言OO第一单元......
  • day4 | 19. 删除链表的倒数第N个结点,24. 两两交换链表中的节点,
    19.删除链表的倒数第N个结点 题目描述 删除链表的倒数第n个结点,并且返回链表的头节点 思路 1.先确定链表结点数,得到length2.再遍历到第length-n个结点上,改......
  • 将VSCode添加到鼠标右键菜单
    第一步:在桌面新建一个以.reg为后缀的注册表文件;文件名称随便例如 1.reg第二步:右击此文件用记事本打开,或者用其他编辑器打开都开启第三步:复制下面的代码粘贴到新建的......
  • 谈谈Java面向对象设计的六大原则
    单一职责原则——SRP开闭原则——OCP里式替换原则——LSP依赖倒置原则——DIP接口隔离原则——ISP迪米特原则——LOD单一职责原则单一职责原则的定义是就一个类......
  • List如何一边遍历,一边删除?
    使用Iterator的remove()方法:每次删除一个元素,都会将modCount的值重新赋值给expectedModCount,这样2个变量就相等了,不会触发java.util.ConcurrentModificationException异常......
  • 再谈编程范式(3):理解面向过程/面向对象/函数式编程的精髓
    面向过程(PO)面向过程是随着VB一起来到我的世界,那个时候会的非常有限,感觉能把程序写出来自己就非常棒了,VB是做那种可视化界面,在工具栏拖个框框放到面板上,然后就在各个事件上写......
  • 【LeeCode】26. 删除有序数组中的重复项
    【题目描述】给你一个 升序排列 的数组 ​​nums​​​ ,请你​​ 原地​​ 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 ......
  • 再谈编程范式(3):理解面向过程/面向对象/函数式编程的精髓
    面向过程(PO)面向过程是随着VB一起来到我的世界,那个时候会的非常有限,感觉能把程序写出来自己就非常棒了,VB是做那种可视化界面,在工具栏拖个框框放到面板上,然后就在各个事件上......
  • elasticsearch添加拼音分词搜索
    分词器是es当中的一个组件,通俗来讲,就是将搜索的条件按照语义进行拆分,分词为多个词语,es会讲text格式的字段按照分词器的结果进行分词,并编排成倒排索引,正因为如此,es的查询速......
  • 19. 删除链表的倒数第 N 个结点
    19.删除链表的倒数第N个结点给你一个链表,删除链表的倒数第n个结点,并且返回链表的头结点。示例1:输入:head=[1,2,3,4,5],n=2输出:[1,2,3,5]示例2:输入:head=......