Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从用户输入的ArrayList中获取值?_Java - Fatal编程技术网

Java 如何从用户输入的ArrayList中获取值?

Java 如何从用户输入的ArrayList中获取值?,java,Java,用户将看到的问题是“您想要哪辆车?” 我希望能够调用数组列表中的特定汽车,并将其打印在一行上 我使用了一个if语句,以便用户输入特定的字母,它将自己从数组中吐出特定的car ArrayList<Car> carList = new ArrayList<Car>(); carList.add(new Car("Nikolai", "Model S", 2017, 54999.90)); carList.add(new Car("Fourd", "Escapade", 201

用户将看到的问题是“您想要哪辆车?” 我希望能够调用数组列表中的特定汽车,并将其打印在一行上

我使用了一个if语句,以便用户输入特定的字母,它将自己从数组中吐出特定的car

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));
}
ArrayList carList=new ArrayList();
carList.add(新车(“Nikolai”,“S型”,2017年,54999.90));
carList.add(新车(“Fourd”,“Escapade”,2017年,31999.90));
carList.add(新车(“丘伊”,“克尔维特”,2017年,44989.90));
carList.add(新使用的汽车(“Hyonda”,“RichardPryor”,2015,14795.5035987.6));
carList.add(新使用的CAR(“GC”,“Chirpus”,2013,8500.00,12345.00));
carList.add(新使用的CAR(“GC”,“威瑟尔”,2016年,14450.00,3500.3));
字符串userInput=“”;
对于(汽车列表:carList){
System.out.printf(theList.getMake()+“\t”+theList.getModel()+“\t”+theList.getYear()+“\t”+“$”+theList.getPrice());
}
System.out.println(“您想要哪辆车?(请键入名称)”;
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或Witherell?”);
}
if(userInput.equalsIgnoreCase(“chr”)){
System.out.println(carList.get(4));
}否则
System.out.println(carList.get(5));
}

我希望System.out.println(carList.get())在for循环运行时能够打印出相应的数组列表。(我知道for循环是有效的…

正如@trarry Wombat在评论中提到的,如果您真的想使用
ArrayList
最简单的方法是实现/覆盖
toString
方法。由于您说
toString
方法当前只打印汽车的名称,因此您可以将其更改为如下内容:

public String toString() {
    return this.getMake() + "\t " + this.getModel() + "\t " + this.getYear() + "\t "+ "$" + this.getPrice();
}
更清楚地说,一辆能正常工作的
汽车可能如下所示:

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));
        }
    }
}

toString
方法是必需的,因为否则,当您从
ArrayList
获取
对象hashcode值时,您只需打印
Car
对象hashcode值。通过重写
toString
方法,java编译器将调用
toString
方法并打印封装在
Car
中的信息。希望这个例子有助于澄清问题

如果要基于“key”输入字符串提取对象,最好不要使用ArrayList,而是使用某种类型的映射,例如
HashMap
。然后,您可以通过
map.get(keyString)
轻松获得合适的汽车。为什么不使用
map
?@ScaryWombat:GMTAWell,说明中说将所有保养和二手车实例存储在同一个arrayList中。我只是想,因为我必须使用arrayList,所以我必须从中提取。
System.out.println(carList.get(0))-我假设您已覆盖
到字符串
public String toString() {
    return super().toString();
}