黑马程序员技术交流社区
标题:
集合电脑
[打印本页]
作者:
郝梦伟
时间:
2019-9-22 12:47
标题:
集合电脑
public class Computer {
private String brand;
private double price;
private String size;
public Computer() {
}
public Computer(String brand, double price, String size) {
this.brand = brand;
this.price = price;
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
@Override
public String toString() {
return "Computer{" +
"brand='" + brand + '\'' +
", price=" + price +
", size='" + size + '\'' +
'}';
}
}
public class Test {
//主方法
public static void main(String[] args) {
//创建一个集合
ArrayList<Computer> list = new ArrayList<Computer>();
Computer c1= new Computer("Apple", 11980.0, "13寸");
Computer c2= new Computer("Dell", 5200.0, "15.5寸");
Computer c3= new Computer("Acer", 4999.0, "15寸");
//将对象添加到集合中
list.add(c1);
list.add(c2);
list.add(c3);
for (int i = 0; i <list.size() ; i++) {
Computer computer = list.get(i);
if(computer.getPrice()>5000){
System.out.println(computer.getBrand()+","+computer.getPrice()+","+computer.getSize());
}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2