用户会看到的问题是“您想要哪辆车?” 而且我希望能够在阵列列表中调用该特定汽车并将其打印在一行上。
我使用的是if语句,以便用户键入某些字母,它将自己从阵列中吐出特定的汽车。
ArrayList<Car> carList = new ArrayList<Car>();
carList.add(new Car("Nikolai", "Model S", 2017, 54999.90));
carList.add(new Car("Fourd", "Escapade", 2017, 31999.90));
carList.add(new Car("Chewie", "Corvette", 2017, 44989.90));
carList.add(new UsedCar("Hyonda", "RichardPryor", 2015, 14795.50, 35987.6));
carList.add(new UsedCar("GC", "Chirpus", 2013, 8500.00, 12345.00));
carList.add(new UsedCar("GC", "Witherell", 2016, 14450.00, 3500.3));
String userInput = "";
for (Car theList : carList) {
System.out.printf(theList.getMake() + "\t " + theList.getModel() + "\t " + theList.getYear() + "\t "+ "$" + theList.getPrice());
}
System.out.println("Which car would you like? (Please type the name)");
userInput = scnr.nextLine();
if (userInput.equalsIgnoreCase("ni")) {
System.out.println(carList.get(0));
} else if (userInput.equalsIgnoreCase("fo")) {
System.out.println(carList.get(1));
} else if (userInput.equalsIgnoreCase("ch")) {
System.out.println(carList.get(2));
} else if (userInput.equalsIgnoreCase("hy")) {
System.out.println(carList.get(3));
} else if (userInput.equalsIgnoreCase("qu")) {
System.out.println(carList.get(6));
if (userInput.equalsIgnoreCase("gc")) {
System.out.println("Chripus or Witherell?");
}
if (userInput.equalsIgnoreCase("chr")) {
System.out.println(carList.get(4));
} else
System.out.println(carList.get(5));
}
我希望在for循环运行时,System.out.println(carList.get())能够打印出相应的数组列表。 (我知道for循环可以使用...)
答案 0 :(得分:1)
正如@Scary Wombat在评论中提到的那样,如果您真的想使用ContextCompat.startForegroundService()
,最简单的方法是实现/覆盖ArrayList
方法。由于您说toString
方法当前仅显示汽车的名称,因此您可以将其更改为以下形式:
toString
更清楚地说,可以正常使用的public String toString() {
return this.getMake() + "\t " + this.getModel() + "\t " + this.getYear() + "\t "+ "$" + this.getPrice();
}
如下:
Car
假设import java.util.ArrayList;
import java.util.Scanner;
class Car {
private String make;
private String model;
private int year;
private double price;
public Car(String make, String model, int year, double price) {
this.make = make;
this.model = model;
this.year = year;
this.price = price;
}
public String getMake() {
return this.make;
}
public String getModel() {
return this.model;
}
public int getYear() {
return this.year;
}
public double getPrice() {
return this.price;
}
public String toString() {
return this.getMake() + "\t " + this.getModel() + "\t " + this.getYear() + "\t "+ "$" + this.getPrice();
}
public static void main(String[] args) {
ArrayList<Car> carList = new ArrayList<Car>();
carList.add(new Car("Nikolai", "Model S", 2017, 54999.90));
carList.add(new Car("Fourd", "Escapade", 2017, 31999.90));
carList.add(new Car("Chewie", "Corvette", 2017, 44989.90));
String userInput = "";
System.out.println("Which car would you like? (Please type the name)");
Scanner scnr = new Scanner(System.in);
userInput = scnr.nextLine();
if (userInput.equalsIgnoreCase("ni")) {
System.out.println(carList.get(0));
} else if (userInput.equalsIgnoreCase("fo")) {
System.out.println(carList.get(1));
} else if (userInput.equalsIgnoreCase("ch")) {
System.out.println(carList.get(2));
}
}
}
继承自UsedCar
,您还可以将Car
中的toString
方法改写为:
UsedCar
此public String toString() {
return super().toString();
}
方法是必需的,因为否则,当您从toString
Car
get
对象时,只是打印ArrayList
对象哈希码值。通过覆盖toString
方法,java编译器将调用您的toString
方法并打印封装在Car
中的信息。希望这个例子有助于清理问题!