package com.zhao.test2;
public class Phone {
private String logo;
private Integer price;
public Phone() {
}
public Phone(String logo, Integer price) {
this.logo = logo;
this.price = price;
}
/**
* 获取
* @return logo
*/
public String getLogo() {
return logo;
}
/**
* 设置
* @param logo
*/
public void setLogo(String logo) {
this.logo = logo;
}
/**
* 获取
* @return price
*/
public Integer getPrice() {
return price;
}
/**
* 设置
* @param price
*/
public void setPrice(Integer price) {
this.price = price;
}
public String toString() {
return "Phone{logo = " + logo + ", price = " + price + "}";
}
}
package com.zhao.test2;
import java.util.ArrayList;
public class PhoneTest {
public static void main(String[] args) {
ArrayList<Phone> arrayList=new ArrayList<>();
Phone p1=new Phone("小米",1999);
Phone p2=new Phone("三星",2999);
Phone p3=new Phone("华为",3999);
arrayList.add(p1);
arrayList.add(p2);
arrayList.add(p3);
ArrayList<Phone> newArrayList=getCheapPhone(arrayList);
for (int i = 0; i < newArrayList.size(); i++) {
Phone phone=newArrayList.get(i);
System.out.println(phone.getLogo()+" "+phone.getPrice());
}
}
//写一个方法将价格低于三千的手机信息返回
public static ArrayList<Phone> getCheapPhone(ArrayList<Phone> arrayList){
ArrayList<Phone> newArrayList=new ArrayList<>();
for (int i = 0; i < arrayList.size(); i++) {
Phone phone=arrayList.get(i);
int price=phone.getPrice();
if (price<3000){
newArrayList.add(phone);
}
}
return newArrayList;
}
}
标签:price,练习,public,Phone,logo,JavaSE,arrayList,ArrayList From: https://www.cnblogs.com/javaHane/p/17181209.html