主要代码:
public class GetPhonePriceTest {
public static void main(String[] args) {
ArrayList<GetPhonePrice> list = new ArrayList<>();
GetPhonePrice getPhonePrice1 = new GetPhonePrice("小米", 1999);
GetPhonePrice getPhonePrice2 = new GetPhonePrice("苹果", 7999);
GetPhonePrice getPhonePrice3 = new GetPhonePrice("锤子", 2999);
list.add(getPhonePrice1);
list.add(getPhonePrice2);
list.add(getPhonePrice3);
ArrayList<GetPhonePrice> phoneInfoList = contains(list, 3000);
for (int i = 0; i < phoneInfoList.size(); i++) {
System.out.println(phoneInfoList.get(i).getBrand() + "," + phoneInfoList.get(i).getPrice());
}
}
public static ArrayList<GetPhonePrice> contains(ArrayList<GetPhonePrice> list, int num) {
ArrayList<GetPhonePrice> newList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getPrice() < num) {
newList.add(list.get(i));
}
}
return newList;
}
}
JavaBean:
public class GetPhonePrice {
private String brand;
private int price;
public GetPhonePrice() {
}
public GetPhonePrice(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;
}
}