添加手机对象并返回要求的数据

java 复制代码
import java.util.ArrayList;

/**
 * @author gyf
 * @ClassName Test
 * @Date 2024/8/3 23:54
 * @Version V1.0
 * @Description :
 */
public class Test {
    public static void main(String[] args) {
        ArrayList<Phone> list = new ArrayList<>();
        Phone phone1 = new Phone("小木", 1000);
        Phone phone2 = new Phone("小红", 8000);
        Phone phone3 = new Phone("小左", 5000);
        list.add(phone1);
        list.add(phone2);
        list.add(phone3);
        ArrayList<Phone> phones = searchInfo(list);
        for (int i = 0; i < phones.size(); i++) {
            System.out.println(phone1.getBrand() + "," + phone1.getPrice());
        }
    }

    public static ArrayList<Phone> searchInfo(ArrayList<Phone> list) {
        ArrayList<Phone> phones = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Phone phone = list.get(i);
            int price = phone.getPrice();
            if (price < 3000) {
                phones.add(phone);
            }
        }
        return phones;
    }
}
java 复制代码
/**
 * @ClassName Phone
 * @author gyf
 * @Date 2024/8/3 23:47
 * @Version V1.0
 * @Description :
*/
public class Phone {
    private String brand;
    private int price;

    public Phone() {
    }

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

    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;
    }
}
相关推荐
ZStack开发者社区12 分钟前
ZStack文档DevOps平台建设实践
java·大数据·开发语言·devops
异常君20 分钟前
揭秘 Spring 验证机制:为何@Validated 在 Controller 层生效却在 Service 层失效?
java·后端·spring
Yharim23 分钟前
ruoyi-cloud分页是怎么回事
java·后端·微服务
掉鱼的猫23 分钟前
史上最强的 Java Solon v3.2.0 发布(并发高 700%;内存省 50%)
java·spring·mcp
luckilyil25 分钟前
springboot自定义starter(避坑教学)
java·spring boot·spring
秋书一叶29 分钟前
Java工具类——实体类列表写入excel
java·开发语言·excel
MobiCetus31 分钟前
Linux Kernel 9
java·linux·运维·服务器·windows·ubuntu·gnu
卷不动的打工仔1 小时前
Ubuntu利用docker搭建Java相关环境记录(二)
java·ubuntu·docker
xcLeigh1 小时前
HTML5好看的水果蔬菜在线商城网站源码系列模板5
java·前端·源码·html5
春_1 小时前
IP范围转IP掩码
java·服务器·网络·tcp/ip