51,410
社区成员
发帖
与我相关
我的任务
分享
这样可以了吧import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Car car1 = new Car("00001", "car01", 1, 2, 100000);
Car car2 = new Car("00002", "car02", 2, 2, 200000);
Car car3 = new Car("00003", "car02", 3, 4, 300000);
Car car4 = new Car("00004", "car01", 4, 4, 200000);
List<Car> list = new ArrayList<Car>();
list.add(car1);
list.add(car2);
list.add(car3);
list.add(car4);
// 查看年限为2年的
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getAge() == 2) {
System.out.println(list.get(i));
}
}
System.out.println("---------------------------");
// 根据注册码00001查看
for (int i = 0; i < list.size(); i++) {
if ("00001".equals(list.get(i).getId())) {
System.out.println(list.get(i));
}
}
}
}
class Car {
public Car() {
}
public Car(String id, String busiName, int carType, int age, double price) {
this.id = id;
this.busiName = busiName;
this.carType = carType;
this.age = age;
this.price = price;
}
// 车:注册码,生产商,车型,年限,价格
private String id;
private String busiName;
private int carType;
private int age;
private double price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getCarType() {
return carType;
}
public void setCarType(int carType) {
this.carType = carType;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getBusiName() {
return busiName;
}
public void setBusiName(String busiName) {
this.busiName = busiName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "注册码:" + id + ",生产商:" + busiName + ",类型:" + carType + ",年限:"
+ age + ",价格:" + price;
}
}
运行结果:
注册码:00001,生产商:car01,类型:1,年限:2,价格:100000.0
注册码:00002,生产商:car02,类型:2,年限:2,价格:200000.0
---------------------------
注册码:00001,生产商:car01,类型:1,年限:2,价格:100000.0