为什么我的foreach循环没有返回任何东西?

时间:2015-03-29 18:56:02

标签: java foreach

我的代码的目的是让用户输入汽车的名称。代码将使用for循环搜索数组列表并打印出用户输入的汽车。代码编译,但不打印任何内容。这是代码:

public class searchList
{  
    private static inventory inventory = new inventory();
    private static ArrayList<engineSpecs> list = inventory.getList(); 
    public void searchList()
    { 
        //Declarations
        Scanner scan = new Scanner(System.in); 
        String search = new String();
        String car = new String();

        //Prompts user to enter car name        
        System.out.println ("Enter car name: ");    
        car = scan.nextLine();

        //Searches array list and prints car
        for (engineSpecs item: list)
        {  
            if (item.equals(car))
            {
                System.out.println (item);
            }
        }  
    }
}

数组列表在另一个类中声明。这是班级:

 public class inventory
 { 
    public static ArrayList<engineSpecs> list = newArrayList<engineSpecs>();
    public inventory()
    {                                                   
        //Adds objects into array list
    engineSpecs astonmartin = new engineSpecs("Aston Martin", "Vanquish", 350000, 11, "Gray",
                                     2015, 565, 457, "automatic");
    engineSpecs ferrari = new engineSpecs ("Ferrari", "458 Italia", 240000, 13, "Red", 2015, 
                                       570, 398, "automatic"); 
    list.add(astonmartin);
    list.add(ferrari);   
    }
    public static ArrayList<engineSpecs> getList()
    {
        //getter method
        return list;
    } 
}

我的engineSpecs类只是一个简单的构造函数。我正在使用它,所以我可以实例化这个构造函数的对象并将其保存到数组列表中。

public class engineSpecs
{
    private int HP;
    private int torque;
    private String transmission;
    private int year;
    private int MPG;
    private int price;
    private String model;
    private String color;
    private String manufacturer;
    public engineSpecs (String manufacturerName, String modelName,int stickerPrice, int MPGe,
    String extColor, int yearNum, int BHP, int torquelbs, String transmissionType) 
    {
        HP = BHP;
        torque = torquelbs;
        transmission = transmissionType;
        year = yearNum;
        MPG = MPGe;
        price = stickerPrice;
        model = modelName;
        color = extColor;
        manufacturer = manufacturerName;
    }
    public String toString()
    {
        String result = "Manufacturer: " + manufacturer + "\n";
        result += "Model: " + model + "\n";
        result += "Price: " + price + "\n";
        result += "Estimated MPG: " + MPG + "\n";
        result += "Color: " + color + "\n";
        result += "Year: " + year + "\n";
        result += "Horse power: " + HP + "\n";
        result += "Torque: " + torque + "\n";
        result += "Transmission: " + transmission + "\n";

        return result;
    }
}

2 个答案:

答案 0 :(得分:0)

您正在使用.equals()来查找在内存中是同一个对象之前不会返回true的对象。您需要一个比较名称的字符串值的新方法。您还需要toString()方法来打印对象的字符串转换。

您的engineSpecs类应该如下所示:

public class engineSpecs{
    public String name;
    public engineSpecs(String s){
        this.name = s;
    }

    public boolean is(String car){
        return this.name.equals(car);
    }

    public String toString(){
        return this.name;
    }
}

更新

将您的if语句更改为:

if (item.is(car))
{
    System.out.println (item);
}

engineSpecs中定义此方法:

public boolean is(String manufacturerName){
    return this.manufacturer.equals(manufacturerName);
   //  need to compare more values if you want to compare more properties
}

答案 1 :(得分:-2)

您可以尝试重新定义班级equals()中的方法engineSpecs,以便item.equals(car)行正常工作。

更多信息here