java_对象数组练习

练习一

练习二 

练习三

练习四

1、再添加一个学生对象,并对其学号的唯一性判断

 2、遍历所有学生信息

3、通过id删除学生信息  删除完后接着遍历学生信息

 4、查询id为3的学生,如果存在 则年龄+1

 5、test中该练习完整代码


练习一

要求:

//javaBean

public class arr_goods {
    private String id;
    private  String name;
    private  double price;
    private  int count;

    public arr_goods() {
    }

    public arr_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;
    }
}
public class root {
    public static void main(String[] args) {
        //创建第一个数组
        arr_goods[] arr = new arr_goods[3];

        //创建第二个数组
        arr_goods g1 = new arr_goods("0001","红牛",5,2000);
        arr_goods g2 = new arr_goods("0002","冰红茶",3,3000);
        arr_goods g3 = new arr_goods("0003","可口可乐",3,5000);

        //将商品放在数组中
        arr[0] = g1;
        arr[1] = g2;
        arr[2] = g3;

        //遍历
        for (int i = 0; i <arr.length ; i++) {
            arr_goods goods = arr[i];
            System.out.println(goods.getId()+" 、"+goods.getName()+"、"+goods.getPrice()+"、"+goods.getCount());
        }
    }

 执行效果:

 

练习二 

 

//javaBean

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

    public Root() {
    }

    public Root(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 test {
    public static void main(String[] args) {
        //定义一个数组 用于存储3个汽车对象
        Root[] arr = new Root[3];

        //创建汽车对象 键盘输入数据
        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < arr.length; i++) {
            //创建汽车对象
            Root c = new Root();

            //录入品牌
            System.out.println("请输入第"+(i+1)+"辆车的品牌:");
            String brand = sc.next();
            c.setBrand(brand);

            //录入价格
            System.out.println("请输入第"+(i+1)+"辆车的价格:");
            int price = sc.nextInt();
            c.setPrice(price);

            //录入录入颜色
            System.out.println("请输入第"+(i+1)+"辆车的颜色:");
            String color = sc.next();
            c.setColor(color);

            //将汽车对象添加到数组中
            arr[i] = c;

        }
        //遍历数组
        for (int i = 0; i <arr.length ; i++) {
            Root car = arr[i];
            System.out.println(car.getBrand()+","+car.getPrice()+"w"+","+car.getColor());
        }
    }
}

 执行效果:

 

练习三

//javaBean

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

    public Root() {
    }

    public Root(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;
    }
}
public class test {
    public static void main(String[] args) {
        //创建一个数组
        Root[] arr = new Root[3];
        //创建手机对象
        Root phone1 = new Root("小米",2999,"蓝色");
        Root phone2 = new Root("苹果",4999,"灰色");
        Root phone3 = new Root("华为",3999,"白色");

        //把手机对象装进数组
        arr[0] = phone1;
        arr[1] = phone2;
        arr[2] = phone3;

        //获取手机价格求平均价格
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            Root phone = arr[i];
            phone.getPrice();
            //累加价格
            sum += phone.getPrice();
        }
        //计算平均值
        int avg = sum / arr.length;
        //打印输出
        System.out.println("手机平均价格为:"+avg);
    }
}

 执行效果:

 

 

练习四

//javaBean
public class Root {
    private int id;
    private String name;
    private int age;

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

    public Root() {
    }

    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 class test {
    public static void main(String[] args) {
        //创建学生数组
        Root[] student_arr = new Root[3];
        //创建学生对象 并添加到数组中
        Root s1 = new Root(001,"小明",19);
        Root s2 = new Root(002,"小张",17);
        Root s3 = new Root(003,"小黄",20);

        student_arr[0] = s1;
        student_arr[1] = s2;
        student_arr[2] = s3;
    }
}

 

要求:

1、再添加一个学生对象,并对其学号的唯一性判断

//创建s4学生对象
        Root s4 = new Root(004,"李四",21);
        //学号唯一性的判断 在外定义一个静态方法来判断唯一性
        boolean a_b = pil(student_arr,s4.getId());
        if (a_b){
            //id存在 不用添加
            System.out.println("当前id重复!!!!");
        }else {
            //不存在则将对象添加到数组 在外定义一个方法判断数组已经存了几个元素


            int count = get_long(student_arr);
            if (count == student_arr.length){
                //数组已经存满
                //创建一个数组 长度为老数组长度+1
                Root[] newArr = creat_arr(student_arr);
                //将s4 存入
                newArr[count] = s4;

            }else {
                //数组没有存满
                student_arr[count] = s4;
            }
        }
public static Root[] creat_arr(Root[] student_arr){
        Root[] new_arr = new Root[student_arr.length+1];
        //将老数组对象添加到新数组中
        for (int i = 0; i <student_arr.length ; i++) {
            new_arr[i] = student_arr[i];
        }
        return new_arr;
    }

    public static int get_long(Root[] student_arr){
        //定义一个计数器 用于统计
        int count = 0;
        for (int i = 0; i <student_arr.length ; i++) {
            if (student_arr[i] != null){
                count++;
            }
        }
        return count;
    }

    public static boolean pil(Root[] student_arr,int id){
        for (int i = 0; i <student_arr.length ; i++) {
            //依次获得学生对象
            Root student = student_arr[i];
            if (student != null){
                //获取id
                int stu_id = student.getId();
                //比较
                if (stu_id == id){
                    return true;
                }
            }
        }
        //循环完之后 如果没有找到一样的 则id不存在 返回false
        return false;
    }

 2、遍历所有学生信息

            if (count == student_arr.length){
                //数组已经存满
                //创建一个数组 长度为老数组长度+1
                Root[] newArr = creat_arr(student_arr);
                //将s4 存入
                newArr[count] = s4;
                print_for(newArr);

            }else {
                //数组没有存满
                student_arr[count] = s4;
                print_for(student_arr);
            }
public static void print_for(Root[] student_arr){
        for (int i = 0; i <student_arr.length ; i++) {
            Root stu = student_arr[i];
            if (stu != null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }

        }
    }

  

3、通过id删除学生信息  删除完后接着遍历学生信息

 //要找到Id在数组中的索引
        int index = getIndex(student_arr,1);
        //对应索引
        System.out.println(index);

        if (index >= 0){
            //如果存在则删除
            student_arr[index] = null;
            //遍历数组
            print_for(student_arr);
        }else {
            //如果不存在 删除失败
            System.out.println("当前id不存在 删除失败!");
        }
      //找到id在数组中的索引
    public static int getIndex(Root[] student_arr,int id){
        for (int i = 0; i < student_arr.length; i++) {
            Root sut = student_arr[i];
            //对stu进行非空判断
            if (sut != null){
                int sid = sut.getId();
                if (sid == id){
                    return i;
                }
            }
        }
        //当循环后还没找到则不存在
        return -1;
    }

     //遍历输出方法
    public static void print_for(Root[] student_arr){
        for (int i = 0; i <student_arr.length ; i++) {
            Root stu = student_arr[i];
            if (stu != null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }

        }
    }

         结果1:

 

        结果2:

 

 4、查询id为3的学生,如果存在 则年龄+1

//先找到id为3的学生索引
        int index_stu = getIndex(student_arr,3);
        //判断索引
        if (index_stu >= 0){
            //存在 年龄+1
            Root stud = student_arr[index_stu];
            //得到原来的年龄
            int newAge = stud.getAge() + 1;
            //把+1后的年龄装回对象中去
            stud.setAge(newAge);
            //遍历数组
            print_for(student_arr);

        }else {
            //不存在
            System.out.println("所查找的学生id不存在");
        }
//找到id在数组中的索引
    public static int getIndex(Root[] student_arr,int id){
        for (int i = 0; i < student_arr.length; i++) {
            Root sut = student_arr[i];
            //对stu进行非空判断
            if (sut != null){
                int sid = sut.getId();
                if (sid == id){
                    return i;
                }
            }
        }
        //当循环后还没找到则不存在
        return -1;
    }



    //遍历输出方法
    public static void print_for(Root[] student_arr){
        for (int i = 0; i <student_arr.length ; i++) {
            Root stu = student_arr[i];
            if (stu != null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }

        }
    }

 5、test中该练习完整代码

public class test {
    public static void main(String[] args) {
        //创建学生数组
        Root[] student_arr = new Root[3];
        //创建学生对象 并添加到数组中
        Root s1 = new Root(1,"小明",19);
        Root s2 = new Root(2,"小张",17);
        Root s3 = new Root(3,"小黄",20);

        student_arr[0] = s1;
        student_arr[1] = s2;
        student_arr[2] = s3;

        //创建s4学生对象
        Root s4 = new Root(4,"李四",21);
        //学号唯一性的判断 在外定义一个静态方法来判断唯一性
        boolean a_b = pil(student_arr,s4.getId());
        if (a_b){
            //id存在 不用添加
            System.out.println("当前id重复!!!!");
        }else {
            //不存在则将对象添加到数组 在外定义一个方法判断数组已经存了几个元素


            int count = get_long(student_arr);
            if (count == student_arr.length){
                //数组已经存满
                //创建一个数组 长度为老数组长度+1
                Root[] newArr = creat_arr(student_arr);
                //将s4 存入
                newArr[count] = s4;
                print_for(newArr);

            }else {
                //数组没有存满
                student_arr[count] = s4;
                print_for(student_arr);
            }
        }
        //要找到Id在数组中的索引
        int index = getIndex(student_arr,5);
        //对应索引
        System.out.println(index);

        if (index >= 0){
            //如果存在则删除
            student_arr[index] = null;
            //遍历数组
            print_for(student_arr);
        }else {
            //如果不存在 删除失败
            System.out.println("当前id不存在 删除失败!");
        }


        //先找到id为3的学生索引
        int index_stu = getIndex(student_arr,3);
        //判断索引
        if (index_stu >= 0){
            //存在 年龄+1
            Root stud = student_arr[index_stu];
            //得到原来的年龄
            int newAge = stud.getAge() + 1;
            //把+1后的年龄装回对象中去
            stud.setAge(newAge);
            //遍历数组
            print_for(student_arr);

        }else {
            //不存在
            System.out.println("所查找的学生id不存在");
        }

        
    }
    //找到id在数组中的索引
    public static int getIndex(Root[] student_arr,int id){
        for (int i = 0; i < student_arr.length; i++) {
            Root sut = student_arr[i];
            //对stu进行非空判断
            if (sut != null){
                int sid = sut.getId();
                if (sid == id){
                    return i;
                }
            }
        }
        //当循环后还没找到则不存在
        return -1;
    }



    //遍历输出方法
    public static void print_for(Root[] student_arr){
        for (int i = 0; i <student_arr.length ; i++) {
            Root stu = student_arr[i];
            if (stu != null){
                System.out.println(stu.getId()+","+stu.getName()+","+stu.getAge());
            }

        }
    }

    public static Root[] creat_arr(Root[] student_arr){
        Root[] new_arr = new Root[student_arr.length+1];
        //将老数组对象添加到新数组中
        for (int i = 0; i <student_arr.length ; i++) {
            new_arr[i] = student_arr[i];
        }
        return new_arr;
    }

    public static int get_long(Root[] student_arr){
        //定义一个计数器 用于统计
        int count = 0;
        for (int i = 0; i <student_arr.length ; i++) {
            if (student_arr[i] != null){
                count++;
            }
        }
        return count;
    }

    public static boolean pil(Root[] student_arr,int id){
        for (int i = 0; i <student_arr.length ; i++) {
            //依次获得学生对象
            Root student = student_arr[i];
            if (student != null){
                //获取id
                int stu_id = student.getId();
                //比较
                if (stu_id == id){
                    return true;
                }
            }
        }
        //循环完之后 如果没有找到一样的 则id不存在 返回false
        return false;
    }
}

 执行效果: 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

open_test01

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值