查找手机价格低于3000(返回集合类型)

package com.itheima.test;

import java.util.ArrayList;

public class Test8 {
    public static void main(String[] args) {
        ArrayList<Phone> list = new ArrayList<>();
        //Phone类
        Phone phone1 = new Phone("小米",1000);
        Phone phone2 = new Phone("苹果",3000);
        Phone phone3 = new Phone("锤子",2999);
        //添加
        list.add(phone1);
        list.add(phone2);
        list.add(phone3);
        //调用
        ArrayList<Phone> low = low(list);
        //遍历
        for (int i = 0; i < low.size(); i++) {
            Phone phone = low.get(i);
            System.out.println(phone.getBrand()+","+phone.getPrice());
        }
    }
    //我要干嘛 查询手机信息
    //需要什么才能完成 集合
    //是否返回 返回 返回的也是集合
    public static ArrayList<Phone> low(ArrayList<Phone> list) {
        //要返回不要打印,返回多个结果,代码复用性高,返回一个集合对象
        ArrayList<Phone> resultList =new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Phone phone = list.get(i);
            int price = phone.getPrice();
            if(price<3000){
                //System.out.println(phone.getPrice()+","+phone.getBrand());
                resultList.add(phone);
            }
        }
        //
        return resultList;
    }

}

 

posted on 2023-03-20 18:45  小王努力不秃头  阅读(31)  评论(0)    收藏  举报

导航