• 48.【Java 基础之对象数组(偏难)】


    1.对象数组的定义:

    对象数组就是数组里的每个元素都是类的对象,赋值时先定义对象,然后将对象直接赋给数组就行了。

    2.对象数组的定义格式:

       类名 []类对象 =new 类名[参数];  //定义类对象数组
       类名 类对象 =new 类名(); //定义类对象
    
    • 1
    • 2

    3.基本演练(设置对象数组)

    类文件:

    import java.util.Random;
    public class Sort {
        private String id;
        private String name;
        private double price;
        private int count;
        public Sort() {
        }
        public Sort(String id, String name, double price, int count) {
            this.id = id;
            this.name = name;
            this.price = price;
            this.count = count;
        }
        public String getId() {
            return id;
        }
        public void setId(String id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public double getPrice() {
            return price;
        }
        public void setPrice(double price) {
            this.price = price;
        }
        public int getCount() {
            return count;
        }
        public void setCount(int count) {
            this.count = count;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    主文件:

    import java.util.Random;
    public class hello {
    
        public static void main(String []avgs) {
            Sort []s=new Sort[3];    //设置对象数组的格式:
            s[0]=new Sort("01","水杯",21,20);
            s[1]=new Sort("02","MAYI ",25,20);
            s[2]=new Sort("03","sdsfd",28,30);
            for(int i=0;i<s.length;i++){
                System.out.println(s[i].getName()+" "+s[i].getId()+" "+s[i].getCount()+" "+s[i].getPrice());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.基本演练 (键盘输入对象数组)

    类文件:

    import java.util.Random;
    public class Sort {
    private String breed;
    private int price;
    private String color;
    public Sort(){}
        public Sort(String breed,int price,String color){
        this.breed=breed;
        this.price=price;
        this.color=color;
        }
    
        public String getBreed() {
            return breed;
        }
    
        public void setBreed(String breed) {
            this.breed = breed;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    主文件:

    import java.util.Random;
    import java.util.Scanner;
    public class hello {
    
        public static void main(String []avgs) {
            Scanner sc = new Scanner(System.in);
           Sort []s=new Sort[3];         //设置对象数组
           for(int i=0;i<s.length;i++){
               Sort ss=new Sort();      //设置对象ss
               System.out.println("请输入汽车的品牌:");
               String breed=sc.next();
               ss.setBreed(breed);
               System.out.println("请输入您的价格:");
               int price=sc.nextInt();
               ss.setPrice(price);
               System.out.println("请输入您的颜色:");
               String color=sc.next();
               ss.setColor(color);
               s[i]=ss;     //把ss传给对象数组
           }
           for(int i=0;i<s.length;i++){
               System.out.println(s[i].getBreed()+" "+s[i].getPrice()+" "+s[i].getColor());
           }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    5.对象数组基本演练(求平均值且判断)

    类文件:

    import java.util.Random;
    public class Sort {
    private String breed;
    private int price;
    private String color;
    public Sort(){}
        public Sort(String breed,int price,String color){
        this.breed=breed;
        this.price=price;
        this.color=color;
        }
    
        public String getBreed() {
            return breed;
        }
    
        public void setBreed(String breed) {
            this.breed = breed;
        }
    
        public int getPrice() {
            return price;
        }
    
        public void setPrice(int price) {
            this.price = price;
        }
    
        public String getColor() {
            return color;
        }
    
        public void setColor(String color) {
            this.color = color;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    主文件;

    import java.util.Random;
    import java.util.Scanner;
    public class hello {
        public static void main(String []avgs) {
            Scanner sc = new Scanner(System.in);
           Sort []s=new Sort[3];
           for(int i=0;i<s.length;i++){
               Sort ss=new Sort();
               System.out.println("请输入汽车的品牌:");
               String breed=sc.next();
               ss.setBreed(breed);
               System.out.println("请输入您的价格:");
               int price=sc.nextInt();
               ss.setPrice(price);
               System.out.println("请输入您的颜色:");
               String color=sc.next();
               ss.setColor(color);
               s[i]=ss;
           }
           double sum=0.0;
            for(int i=0;i<s.length;i++){
                sum+=s[i].getPrice();
            }
            System.out.println("平均值为:"+(sum/3));
            int count=0;
            for(int i=0;i<s.length;i++){
                if((sum/3)>=s[i].getPrice()){
                    count++;
                    System.out.println(s[i].getBreed()+" "+s[i].getPrice()+" "+s[i].getColor());
                }
            }
            System.out.println("比平均值低的有:"+count+"个");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    6.超级综合训练:

    定义一个长度为3的数组,数组储存1~3名学生对象作为初始数据,学生对象的学号、姓名各不相同。
    学生的属性:学号,兴民,年龄。
    要求1.再次添加一个学生对象,并在添加的时候进行唯一性判断.
    要求2:添加完毕之后,遍历所有学生的信息
    要求3:通过id删除学生信息,如果存在,则删除。如果不存在则提示失败
    要求4:删除完毕之后,遍历所有的学生信息。
    要求5:查询数组id为 21032114 的学生,如果存在,则将他的年龄+1;

    类文件:

    import java.util.Random;
    public class Sort {
        private String name;
        private int  number;
        private int age;
        public Sort() {
        }
        public Sort(String name, int number, int age) {
            this.name = name;
            this.number = number;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getNumber() {
            return number;
        }
        public void setNumber(int number) {
            this.number = number;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    主文件:
    1.判断是否学号相等:

     public static boolean judgeSimlar(Sort []s,int id){
            for(int i=0;i<s.length;i++){
                if(s[i].getNumber()==id){
                    return true;
                }
            }
            return false;
        }  //
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.判断原始数组是否已经沾满

    public static int  Count(Sort[]s){
            int count=0;
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){        //假如说不为空的前提下,有多少个空间
                count++;}
            }
            return count;
        } //判断下表数量
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3.创造新的数组

     public static Sort[] CreatArr(Sort[]s){
            Sort []newArr=new Sort[s.length+1];
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){
                newArr[i]=s[i];}
            }
            return newArr;
        } //创造心得数组
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.打印遍历:

    public static void show(Sort[]s){
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){
                System.out.println(s[i].getName()+" "+s[i].getNumber()+" "+s[i].getAge());
            }
            }
        }//打印输出
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5.删除元素:

     public static void cc(Sort[]s,int id){
            if(clear(s,id)<0){  //假如说不存在
                System.out.println("不存在删除失败!");
            }
            if(clear(s,id)>=0){
                s[clear(s,id)]=null;
                show(s);
            }
        }//删除元素
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    6.判断id是否相等

     public static int clear(Sort[]s,int id){
            int i=0;
            for(i=0;i<s.length;i++){
                if(s[i]!=null){
                    if(s[i].getNumber()==id){
                        return i;
                    }
                }
            }
            return -1;
        }//判断是否是相同的id
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    7.增加年龄

     public static void add(Sort[]s,int id){
            int number=0;
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){
                    if(clear(s,id)<0){
                        System.out.println("不存在");
                    }
                    else {
                        number=s[i].getAge();
                        number+=1;
                    }
                }
            }
            System.out.println(number);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    完整主文件:

    
    public class hello {
        public static void main(String []avgs) {
        Sort []s=new Sort[3];
        s[0]=new Sort("小明",21032114,25);
        s[1]=new Sort("小红",21032115,20);
        s[2]=new Sort("小黑",21032116,23);
        Sort s4=new Sort("小紫",21032118,20);
        int id=s4.getNumber();
        //进行判断是否有相同的id,有相同的就让他们重新输入.不相同的话继续判断里面的内存问题,然后再进行输出
            boolean flag=judgeSimlar(s,id);
            if(flag){
                System.out.println("您输入的信息已经存在,请您在输入一个数字!");
            }
            else{
                //假如说不相同的话,就要添加数组了,此时我们要考虑一下内存的问题,如果内存狗的话那么就直接写,内存不够的话就要重新加u
                if(s.length==Count(s)){    //假如说相等的话,就要新加一个数组
                    Sort []newArr=CreatArr(s);//实际上是 :new Sort[s.length+1];
                    newArr[newArr.length-1]=s4;
                   // cc(newArr,21032115);
                    add(newArr,21032115);
                }
                else{     //假如说没有满的情况下,那么我们就把它直接操作,下标就是我们添加的位置
                        s[Count(s)]=s4;
                    //cc(s,21032115);  删除
                    add(s,21032115);
                }
            }
        }
        public static void add(Sort[]s,int id){
            int number=0;
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){
                    if(clear(s,id)<0){
                        System.out.println("不存在");
                    }
                    else {
                        number=s[i].getAge();
                        number+=1;
                    }
                }
            }
            System.out.println(number);
        }
        public static int clear(Sort[]s,int id){
            int i=0;
            for(i=0;i<s.length;i++){
                if(s[i]!=null){
                    if(s[i].getNumber()==id){
                        return i;
                    }
                }
            }
            return -1;
        }//判断是否是相同的id
        public static void cc(Sort[]s,int id){
            if(clear(s,id)<0){  //假如说不存在
                System.out.println("不存在删除失败!");
            }
            if(clear(s,id)>=0){
                s[clear(s,id)]=null;
                show(s);
            }
    
    
        }//删除元素
        public static void show(Sort[]s){
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){
                System.out.println(s[i].getName()+" "+s[i].getNumber()+" "+s[i].getAge());
            }
            }
        }//打印输出
        public static Sort[] CreatArr(Sort[]s){
            Sort []newArr=new Sort[s.length+1];
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){
                newArr[i]=s[i];}
            }
            return newArr;
        } //创造心得数组
        public static int  Count(Sort[]s){
            int count=0;
            for(int i=0;i<s.length;i++){
                if(s[i]!=null){        //假如说不为空的前提下,有多少个空间
                count++;}
            }
            return count;
        } //判断下表数量
        public static boolean judgeSimlar(Sort []s,int id){
            for(int i=0;i<s.length;i++){
                if(s[i].getNumber()==id){
                    return true;
                }
            }
            return false;
        }  //
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
  • 相关阅读:
    62、 忠北国立大学计算机科学系:FingerNet-专门用于细致MI分类的神经网络模型
    【面试题】2022前端面试真题
    两个读书笔记:springboot+vue.js分布式组件全栈开发训练营 + 大数据开发基础
    基于单片机的智能考勤机(论文+源码)
    Maven知识【IDEA使用Maven&依赖管理】第三章
    谷粒商城--分布式高级篇P129~P339(完结)
    Remote Desktop Service (RDS) 远程桌面服务漏洞简介 BlueKeep DejaBlue
    Windows 同时安装 MySQL5 和 MySQL8 版本
    24 行为型模式-访问者模式
    Go Web项目 接口开发全流程
  • 原文地址:https://blog.csdn.net/qq_69683957/article/details/126553670