ArrayOfObjects

文章详细介绍了如何在Java中定义和操作包含不同属性(如id、名字、价格、品牌、颜色等)的对象数组,包括商品、汽车、手机以及学生对象,并展示了添加、删除和计算平均值等操作。
摘要由CSDN通过智能技术生成

一:定义包含“id”,“名字”,“价格”的商品,将其存入数组中

Goods:

public class Goods {
    private String id;
    private String name;
    private double price;
    private int count;
    public Goods(){

    }

    public Goods(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;
    }
}

主类:

import java.util.Scanner;

public class ArrayOfObjects {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Goods []arr=new Goods[3];
        Goods g1=new Goods("1","A",1000,1000);
        Goods g2=new Goods("2","B",2000,2000);
        Goods g3=new Goods("2","C",3000,3000);
        arr[0]=g1;
        arr[1]=g2;
        arr[3]=g3;
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

二:定义包含“品牌”,“价格”,“颜色”的汽车,数据通过键盘录入而来,将其存入数组中

Cars:

public class Cars {
    private String brand;
    private int price;
    private String color;

    public Cars(){

    }
    public Cars(String brand, int price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    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;
    }
}

主类:

import java.util.Scanner;

public class ArraysOfObjects {
    public static void main(String[] args) {
        Cars []arr=new Cars[3];
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            Cars c=new Cars();
            String setbrand= sc.next();
            int setprice= sc.nextInt();
            String setcolor= sc.next();
            c.setBrand(setbrand);
            c.setPrice(setprice);
            c.setColor(setcolor);
            arr[i]=c;
        }
        for (int i = 0; i < arr.length; i++) {
            Cars c=arr[i];
            System.out.println(c.getBrand()+" "+c.getPrice()+" "+c.getColor());
        }
    }
}

三:定义包含“品牌”,“价格”,“颜色”的手机对象

计算出三部手机的平均价格

Phones:

public class Phones {
    private String brand;
    private int price;
    private String color;

    public Phones(){

    }
    public Phones(String brand, int price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    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;
    }
}

主类:

import java.util.Scanner;

public class ArrayOfObjects {
    public static void main(String[] args) {
        Phones []arr=new Phones[3];
        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            Phones p=new Phones();
            String getbrand= sc.next();
            int getprice= sc.nextInt();
            String getcolor= sc.next();
            p.setBrand(getbrand);
            p.setPrice(getprice);
            p.setColor(getcolor);
            arr[i]=p;
        }
        int sum=0;
        for (int i = 0; i < arr.length; i++) {
            Phones p=arr[i];
            sum+=p.getPrice();
        }
        int result=sum/arr.length;
        System.out.println(result);
    }
}

四:定义包含“姓名”,“年龄”,“性别”,“爱好”的女朋友对象

计算出四个女朋友的平均年龄

统计年龄比平均值低的女朋友有几个,并统计出她们的信息

Girlfriend:

public class Girlfriend {
    private String name;
    private int age;
    private String gender;
    private String hobby;

    public Girlfriend(){

    }
    public Girlfriend(String name, int age, String gender, String hobby) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.hobby = hobby;
    }

    public void show(Girlfriend gf){
        System.out.println("姓名为:"+gf.getName()+" 年龄为:"+gf.getAge()+" 性别为:"+gf.getGender()+" 爱好为:"+gf.getHobby());
    }
    public void judge(Girlfriend gf,int ave){
        if (gf.getAge()<ave){
            gf.show(gf);
        }
    }
    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;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }
}

主类:

public class ArrayOfObjects {
    public static void main(String[] args) {
        Girlfriend gf1=new Girlfriend("A",15,"女","玩游戏");
        Girlfriend gf2=new Girlfriend("B",16,"女","追剧");
        Girlfriend gf3=new Girlfriend("C",17,"女","品尝美食");
        Girlfriend gf4=new Girlfriend("D",18,"女","旅游");
        int sum=gf1.getAge()+gf2.getAge()+gf3.getAge()+gf4.getAge();
        int ave=sum/4;
        System.out.println("平均年龄为:"+ave);
        Girlfriend []arr=new Girlfriend[4];
        gf1.judge(gf1,ave);
        gf2.judge(gf2,ave);
        gf3.judge(gf3,ave);
        gf4.judge(gf4,ave);
    }
}

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

学生的属性:学号,姓名,年龄。

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

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

要求3:通过id删除学生信息。

如果存在,则删除,如果不存在,则提示删除失败。

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

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

Student:

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

    public Student(){

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

    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 int getAge() {
        return age;
    }

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

主类:

public class ArrayOfObjects {
    public static void main(String[] args) {
        Student[] arr=new Student[3];
        Student stu1=new Student("001","A",18);
        Student stu2=new Student("002","B",19);
        Student stu3=new Student("003","c",20);
        arr[0]=stu1;
        arr[1]=stu2;
        arr[2]=stu3;
        Student stu4=new Student("004","D",21);
        //添加及输出学生信息
        boolean flag=judgeID(arr,stu4.getId());
        if(flag){
            System.out.println("当前id已存在,添加失败");
        }
        else{
            int c=count(arr);
            if(c== arr.length){
                Student[] Arr=add(arr);
                Arr[c]=stu4;
                print(Arr);
            }
            else{
                arr[c]=stu4;
                print(arr);
            }
        }
        //删除并输出学生信息
        int index=getIndex(arr,"001");
        if(index>=0){
            arr[index]=null;
            print(arr);
        }
        else{
            System.out.println("当前id不存在,删除失败");
        }
        //使得id为002的学生年龄加1
        int Index=GetIndex(arr,"002");
        if(Index>=0){
            Student stu=arr[Index];
            int age=stu.getAge()+1;
            stu.setAge(age);
            print(arr);
        }
        else{
            System.out.println("当前id不存在,年龄修改失败");
        }
    }
    public static Student[] add(Student[] arr){//如果原本长度为3的数组已经满了,那么就需要一个新的,容量更大的数组来接收多出来的第四个元素
        Student []Arr=new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            Arr[i]=arr[i];
        }
        return Arr;
    }
    public static boolean judgeID(Student[] arr,String id){//判断ID是否已经存在于数组中
        for (int i = 0; i < arr.length; i++) {
            Student stu=arr[i];
            if(stu!=null){//如果stu被赋值为null,即没有元素,程序会报错
                String Getid= stu.getId();
                if(Getid.equals(id)){
                    return true;
                }
            }
        }
        return false;
    }
    public static int count(Student[] arr){//对当前数组中的元素个数进行计数
        int c=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                c++;
            }
        }
        return c;
    }
    public static void print(Student[] arr){//依次打印数组中的数据
        for (int i = 0; i < arr.length; i++) {
            Student stu=arr[i];
            if(stu!=null){
                System.out.println("ID为:"+stu.getId()+" 姓名为:"+stu.getName()+" 年龄为:"+stu.getAge());
            }
        }
    }
    public static int getIndex(Student[] arr,String id){//获取要删除的id的索引
        for (int i = 0; i < arr.length; i++) {
            Student stu=arr[i];
            if(stu!=null){
                String ID= stu.getId();
                if(ID.equals(id)){
                    return i;
                }
            }
        }
        return -1;
    }
    public static int GetIndex(Student[] arr,String id){//获取年龄加1的学生的id的索引
        for (int i = 0; i < arr.length; i++) {
            Student stu=arr[i];
            if(stu!=null){
                String ID= stu.getId();
                if(ID.equals(id)){
                    return i;
                }
            }
        }
        return -1;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值