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

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;
    }
}
相关推荐
日月云棠5 小时前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840826 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide7 小时前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家7 小时前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺7 小时前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户908324602737 小时前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端
桦说编程8 小时前
实战分析 ConcurrentHashMap.computeIfAbsent 的锁冲突问题
java·后端·性能优化
程序员清风12 小时前
用了三年AI,我总结出高效使用AI的3个习惯!
java·后端·面试
beata13 小时前
Java基础-13: Java反射机制详解:原理、使用与实战示例
java·后端
用户03321266636713 小时前
Java 使用 Spire.Presentation 在 PowerPoint 中添加或删除表格行与列
java