ArrayList练习_返回多个数据

package 基础.week2.day9;

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;
    }
}
package 基础.week2.day9;

import java.util.ArrayList;

public class Demo6 {
    public static void main(String[] args) {
        //ArrayList练习,返回多个数据
        ArrayList<Phone> arrayList=new ArrayList<>();

        Phone phone1 = new Phone("小米", 1000);
        Phone phone2 = new Phone("苹果", 2000);
        Phone phone3 = new Phone("菠萝", 5000);

        arrayList.add(phone1);
        arrayList.add(phone2);
        arrayList.add(phone3);

        ArrayList<Phone> phoneInfo = getPhoneInfo(arrayList, 3000);
        for (int i = 0; i < phoneInfo.size(); i++) {
            Phone phone = phoneInfo.get(i);
            System.out.println(phone.getBrand()+","+phone.getPrice());
        }


    }
    public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> arrayList,int price){
        //返回低于price价格的手机
        System.out.println("低于"+price+"价格的手机:");
        ArrayList<Phone> resultList=new ArrayList<>();
        for (int i = 0; i < arrayList.size(); i++) {
            Phone phone = arrayList.get(i);
            int rate = phone.getPrice();
            if (rate<price){
                resultList.add(phone);
            }
        }
        return resultList;
    }
}

低于3000价格的手机:
小米,1000
苹果,2000

Process finished with exit code 0
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值