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
苹果,2000Process finished with exit code 0