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

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;
    }
}
相关推荐
这周也會开心6 分钟前
云服务器安装JDK、Tomcat、MySQL
java·服务器·tomcat
hrrrrb1 小时前
【Spring Security】Spring Security 概念
java·数据库·spring
小信丶1 小时前
Spring 中解决 “Could not autowire. There is more than one bean of type“ 错误
java·spring
周杰伦_Jay2 小时前
【Java虚拟机(JVM)全面解析】从原理到面试实战、JVM故障处理、类加载、内存区域、垃圾回收
java·jvm
程序员小凯5 小时前
Spring Boot测试框架详解
java·spring boot·后端
豐儀麟阁贵6 小时前
基本数据类型
java·算法
_extraordinary_6 小时前
Java SpringMVC(二) --- 响应,综合性练习
java·开发语言
程序员 Harry7 小时前
深度解析:使用ZIP流式读取大型PPTX文件的最佳实践
java
wxweven7 小时前
校招面试官揭秘:我们到底在寻找什么样的技术人才?
java·面试·校招
陈陈爱java8 小时前
新知识点背诵
java