Java对象数组练习,超详细解释,超多案例


public class Goods {

    //定义一个存储三个商品的对象
    //商品的属性id,名字,价格,库存
    //创建三个商品,放到数组当中去
    private  String id;
    private  String name;
    private double price;
    private int count;
    //下面定义getset,构造器
    public Goods (){};


    //声明有参构造器,this关键字有讲,代表引用成员内部变量
    public Goods (String id,String name,double price,int count){
        this.name=name;
        this.count=count;
        this.id=id;
        this.price=price;

    }


    //下面就是get set方法
    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;
    }
}


public class GoodsTest {
    public static void main(String[] args) {
        //定义一个Goods类型的数组用来存储商品
        Goods arr[]=new  Goods[3];//已知的一共有三个商品


        //给构造器传入三个商品的参数
        Goods a=new Goods("华为","P40",4.06,560);
        Goods a1=new Goods("苹果","13",40.06,500);
        Goods a2=new Goods("OPPEO","A5",400.06,530);
        arr[0]=a;//在讲商品的信息录入进去数组当中
        arr[1]=a1;
        arr[2]=a2;
        //循环遍历数组
        for (int i = 0; i < arr.length; i++) {
            //注意如果直接这样的话将会输出的是地址值

            System.out.println(arr[i]);

            //将每一次循环arr[i]的值复制给goods,就比如过当i=1的时候那么数组的内容就为苹果的内容,一一对应赋值过来在进行输出
            Goods goods=arr[i];
            System.out.println("商品的名字为"+goods.getId()+"商品的型号为"+goods.getName()+"商品的价格为"+goods.getPrice()+"商品的价格为"+goods.getCount());
        }





    }
}

二,使用scanner来做这个操作
public class Car {


    //创建一个汽车的数组,只不过这次的是从键盘录入数局的
    private String brand;//品牌
    private int price ;//价格
    private String color;//颜色

    public Car() {
    }

    public Car(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.io.FileOutputStream;
import java.util.Scanner;

public class CarTest {
    public static void main(String[] args) {
        //创建一个汽车的数组,用来存储
        Car arr[] = new Car[3];
        //使用scanner来录入数据
        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < arr.length; i++) {
            //创建car对象用来调用Car的set方法
            //每次循环都将用户所录入的数据记录到set方法
            System.out.println("请输入品牌");

            //创建一个car对象,用来调用set方法,通过用户录入的数据存储到set中,每一次循环都会执行一次就这样以此类推
            //先使用空参来创建
            Car c = new Car();
            String brand=  sc.next();
            c.setBrand(brand);
            System.out.println("请输入价格");
            int price=  sc.nextInt();
            c.setPrice(price);
            System.out.println("请输入颜色");
            String color= sc.next();
            c.setColor(color);

            //arr[i]就表示每次循环的角标一直在增加,表示存储的Car类型的数据,并将该数据赋值给car,相当于就是去跟数据做联系了
            arr[i]=c;

        }
        for (int i = 0; i < arr.length; i++) {
                //将arr[i]每一次的值,都赋值给Car类型的car,并用car去调用Car的get方法来进行输出
               Car car=arr[i];
               System.out.println(car.getBrand()+car.getColor()+car.getPrice());
        }

    }
}

三
public class Phone {
    private String brand;//品牌
    private int price ;//价格
    private String color;//颜色

    public Phone() {
    }

    public Phone(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 javax.swing.*;

public class PhoneTest {
    public static void main(String[] args) {
        //创建一个数组
        Phone arr []=new Phone[3];
        //创建手机的对象三个
        Phone p1 =new Phone("华为",8888,"黑色");
        Phone p2 =new Phone("苹果",6998,"白色");
        Phone p3 =new Phone("小米",1999,"蓝色");
        arr[0]=p1;
        arr[1]=p2;
        arr[2]=p3;
        //求收集价格的平均数
        //先定义一个总和的数
        int sum=0;
        for (int i = 0; i < arr.length; i++) {
           Phone phone = arr[i];
            sum =sum+phone.getPrice();
        }
        System.out.println("所以平均价格是"+sum/3);

    }
}

四
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 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 GirlFriendTest {
    public static void main(String[] args) {
        //定义一个数组,存储女朋友的信息
        GirlFriend[] arr = new GirlFriend[4];
        GirlFriend G1 = new GirlFriend("小妹", 10, "萌妹子", "唱歌");
        GirlFriend G2 = new GirlFriend("二妹", 21, "萌妹子", "跳舞");
        GirlFriend G3 = new GirlFriend("三枚", 54, "萌妹子", "打游戏");
        GirlFriend G4 = new GirlFriend("大妹", 68, "萌妹子", "玩手机");
        arr[0] = G1;
        arr[1] = G2;
        arr[2] = G3;
        arr[3] = G4;

        //计算总年龄和平均年龄
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum = sum + arr[i].getAge();
        }
        int avg = sum / arr.length;

        //统计比平均年龄小的女朋友数量,并打印信息
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            //在这里做一个解释,用GirlFriend来创建的数组也可以去调用GirlFriend的方法
            //比如说arr[0].getAge就表示调用数组角标0中的getAge
            //在循环中相应的换成i就表示循环每一个
            if (arr[i].getAge() < avg) {
                count++;
            }
        }
        System.out.println("比平均年龄小的一共有" + count + "个");

        //打印比平均年龄小的女朋友信息
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].getAge() < avg) {
                System.out.println("姓名:" + arr[i].getName() + ",年龄:" + arr[i].getAge() + ",性别:" + arr[i].getGender() + ",爱好:" + arr[i].getHobby());
            }
        }
    }
}
五
public class Student {
    private int id;

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

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

    public Student() {
    }

    private String name;
    private int age;

}


public class StudentTest {
    private static Student arr[];

    public static void main(String[] args) {
        // 创建一个数据用来存储学生信息
        Student arr[] = new Student[3];
        // 创建学生对象并添加到数组
        Student stu1 = new Student(1, "小明", 18);
        Student stu2 = new Student(2, "小强", 19);
        Student stu3 = new Student(3, "小花", 20);
        // 再将形参的内容赋值到数组当中去
        arr[0] = stu1;
        arr[1] = stu2;
        arr[2] = stu3;
        Student stu4 = new Student(4, "小红", 20);
        // 下面的判断方法写完之后就可以来直接调用
        // 将数组arr和id放入进去,方法的结果会产生一个boolean,将方法的结果在赋值给flag
        boolean flag = contains(arr, stu4.getId());
        // 下面对结果进行判断直接将方法的结果写在里面就行
        if (flag) {
            // 表示id已经存在的,直接进行一个提示就可以了
            System.out.println("当前id:" + stu4.getId() + "重复请更换id");
        } else {
            // 表示id不存在
            // 把stu4添加到数组
            // 1.数组已经存满 只能创建一个新的数组,长度是老数组+1
            // 2.数组没有存满直接添加
            // 所以还需要再去写一个方法来判断
        }
        // 将统计的count调用出来
        int count = getCount(arr);
        // 来做if判断
        if (count == arr.length) {
            // 表示已经存满
            // 需要创建一个新的数组,长度是老数组+1,还需要把老数组拷贝到新数组当中去,用一个新的方法来定义
            // 调用下面的copy方法,方法会返回一个新的数组,现在将他赋值给newarr
            Student newArr[] = copy(arr);
            // 这个地方跟下面那个else的解释一样
            newArr[count] = stu4;
            // 二,遍历学生信息
            // 这个地方有一个坑就是要判断一下是老数组还是新数组我们来定义一个方法printArr

            // 调用下面的数组
            printArr(newArr);
        } else {
            // 没有存满
            /*
             * 这个通过getCount方法获取到的count,就假如说现在数组有三个索引,
             * 用掉了俩 也就是arr [0],arr[1] 被使用掉了,但是他在这不仅仅只是表示这个,他也在表示他是二,
             * 就是在存入的时候可以直接用count来代表数组的角标位置,好好理解一下,我大概解释一下,就按照
             * 上面的解释继续来说,arr[0],arr[1],此时count=2,所以下一个角标可以直接写arr[count]
             */
            // 所以将stu4添加进来
            arr[count] = stu4;
            // 调用下面的数组
            printArr(arr);
        }
        // 三,根据id来删除对应的学生信息
        // 这里就需要用到一个方法来确定这个学生的索引信息getIndex
        int index=getIndex(arr,5);
        if (index >=0){
            //删除代码
              arr[index]=null;
        }else {
            System.out.println("删除失败");
        }
        //四删除完之后遍历数组
        System.out.println();
        printArr1(arr);


    }

    // 添加一个学生,并将id作为唯一性判断
    // 唯一性判断
    // 已存在就添加
    // 不存在就直接可以把学生的对象添加进数组内

    // 分析一下如何判断,和需不需要返回值类型
    // 判断id是否以存在需要将arr的内容传递进去来跟id进行判断,此外返回一个已存在或者是不存在
    public static boolean contains(Student arr[], int id) {
        // 先循环遍历数组
        for (int i = 0; i < arr.length; i++) {
            // 判断arr[i]是否为null,以避免空指针异常
            if (arr[i] != null) {
                // 循环遍历每一次数组中来获取id
                int a = arr[i].getId();
                // 判断id是否相同
                if (a == id) {
                    return true;
                }
            }
        }
        return false;
    }

    public static int getCount(Student[] arr) {
        // 如何来判断数组中有几个元素啊是空的呢?其实这里可以用到null,如果是空的数组的默认一定是null
        // 再来定义一个统计的count
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null) {
                count++;
            }
        }
        // 最后将count返回方法调用处
        return count;
    }

    // 创建一个新的方法copy并将arr数组传递进来
    // 并将返回值类型写成Student类型的数组
    public static Student[] copy(Student arr[]) {
        // 创建一个新的数组等于老数组的长度加1
        Student newArr[] = new Student[arr.length + 1];
        // 再来遍历
        for (int i = 0; i < arr.length; i++) {
            // 将老数组每一次遍历的角标0都赋值给新的数组
            newArr[i] = arr[i];
        }
        return newArr;
    }

    public static void printArr(Student arr[]) {
        for (int i = 0; i < arr.length; i++) {
            // 将arr[i]的值赋值给stu
            // 注意在这里有一个小细节,就是arr[i]如果为空的那么那就是null,null在赋值给stu之后是不能够去调用方法的
            // 所以这里要加一个if判断
            Student stu = arr[i];
            if (stu != null) {
                // 判断stu是否为空就可以知道是新数组还是老数组
                System.out.println(stu.getAge() + "," + stu.getId() + "," + stu.getName());
            }
        }
    }

    public static int getIndex(Student arr[], int id) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null && arr[i].getId() == id) {
                return i;
            }
        }
        return -1; // 表示未找到该ID的学生
    }
    public static void printArr1(Student arr[]) {
        for (int i = 0; i < arr.length; i++) {
            // 将arr[i]的值赋值给stu
            // 注意在这里有一个小细节,就是arr[i]如果为空的那么那就是null,null在赋值给stu之后是不能够去调用方法的
            // 所以这里要加一个if判断
            Student stu = arr[i];
            if (stu != null) {
                // 判断stu是否为空就可以知道是新数组还是老数组
                System.out.println(stu.getAge() + "," + stu.getId() + "," + stu.getName());
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值