如何在Java中搜索数组并检查条件

如何在Java中搜索数组并检查条件,java,arrays,sorting,arraylist,methods,Java,Arrays,Sorting,Arraylist,Methods,我有一系列新车和二手车。我想在数组中搜索颜色为绿色的汽车。方法printAllCarsOfColor(字符串颜色)将返回一个字符串,该字符串显示作为参数给定的颜色的所有车辆的信息。我试过了,但运气不好 我有一个Car类,它是超类,NewCar和UsedCar是子类。只有NewCar类具有Color方法。我不知道如何打印出只有绿色的汽车。我知道Color方法没有为Car类定义 正在寻求帮助。谢谢 我的代码: public class CarDearlerShip { private Car[

我有一系列新车和二手车。我想在数组中搜索颜色为绿色的汽车。方法
printAllCarsOfColor(字符串颜色)
将返回一个字符串,该字符串显示作为参数给定的颜色的所有车辆的信息。我试过了,但运气不好

我有一个Car类,它是超类,
NewCar
UsedCar
是子类。只有
NewCar
类具有
Color
方法。我不知道如何打印出只有绿色的汽车。我知道
Color
方法没有为
Car
类定义

正在寻求帮助。谢谢

我的代码:

public class CarDearlerShip
{
   private Car[] aCar;
   private int count;
   private int car;
  
   public CarDearlerShip()
   {
     aCar = new Car[80];
      count = 0;
      car = 0;  
   }
  
   
   public void addNewCar(String model, int year, int price, String color)
   {
      aCar[count] = new NewCar(model, year, price, color);
      count++;
   }   
   
   public void addUsedCar(String model, int year, int price, boolean rusty)
   {
      aCar[count] = new UsedCar(model, year, price, rusty);
      count++;
   } 
   
   public String printReport()
   {
      String report = "---------------------------------------------\n";
      report += "The list of availabel cars:\n\n";
      
     for (int car = 0; car < count; car++)
      report += aCar[car].toString() + "\n";
      //System.out.println(report);
    return report;
      
   }
   
  public String printAllCarsWithSellingPriceBelow(int price) 
   {
      String printCars = "---------------------------------------------\n";
      printCars += "The price for these cars are: \n\n";
      
      for (int car = 0; car < count; car++)
      {
          if (aCar[car].aPrice < 1000); // The argument for the selling price
          printCars +=aCar[car].getPrice();
      }
         return printCars;       
   
   }
   
   public String printAllCarsOfColor(String color)
   {
      String printColor = "---------------------------------------------\n";
      printColor += "The color : \n\n";
      
      
      for (int car = 0; car < count; car++)
      {
         if (aCar[car].Color()==color);
         printColor +=aCar[car];
         
      }
      return printColor;      
   
   }
}

The Test Class
    
public class TestDealer 
{
    public static void main(String[] args)
    {
        CarDearlerShip dealer= new CarDearlerShip();
        dealer.addNewCar("GM Buick Century", 2004, 200000, "Silver");
        dealer.addUsedCar("Toyota Corolla", 1999, 9000, true);
        dealer.addNewCar("Honda Civic", 2004, 500, "Green");
        dealer.addNewCar("BMW 320i", 2004, 35000, "Black");
        dealer.addUsedCar("Toyota Sienna", 2000, 11000, false);
        
            System.out.println(dealer.printReport());
            System.out.println("**********************************");
        
        //System.out.println(dealer.printAllCarsWithSellingPriceBelow(1000));
            
            System.out.println("****************************************");

        System.out.println(dealer.printAllCarsOfColor("Green"));
        
        
    }

}
public-class-cardeEarlership
{
私家车【】aCar;
私人整数计数;
私家车;
公职人员
{
aCar=新车[80];
计数=0;
car=0;
}
public void addNewCar(字符串型号、整数年份、整数价格、字符串颜色)
{
aCar[计数]=新车(型号、年份、价格、颜色);
计数++;
}   
public void addUsedCar(字符串模型、整数年、整数价格、布尔型rusty)
{
aCar[计数]=新使用的CAR(型号、年份、价格、生锈);
计数++;
} 
公共字符串printReport()
{
字符串报告=“-------------------------------------------------------------\n”;
报告+=“可用标签车辆列表:\n\n”;
对于(int car=0;car
在比较汽车的颜色方法的输出时,需要使用内置的java.lang.String方法。equals()

在代码中有:
if(aCar[car].Color()==Color){

但是它必须是
if(aCar[car].Color().equals(Color)){


基本上,编译器永远不会理解你在说什么,因为字符串变量“Color”只是指向计算机内存中存储值“green”的空间的指针对于该指针,它们将永远不会相同。

共享您的
汽车
UsedCar
NewCar
源代码?看起来像是输入错误:
if(aCar[Car].Color()==Color);
不应该有分号(
)之后。你应该对字符串使用
.equals
。我认为颜色应该是Car的属性,而不是NewCar的属性,这会让事情变得更简单。谢谢你的回复,Nick。我现在理解我的错误。但是,当我试着运行程序时,我得到以下错误:方法color()类型CarYes的未定义。在NewCar/UsedCar类中,您需要创建一个getColor方法。或者,您可以像现有的那样调用它。该方法应采用创建新车时存储的实例变量Color并返回它。然后,您的程序应能正常工作。在NewCar/UsedCar c中,应类似于此lass.public String getColor(){return this.color;}我完全同意@NomadMaker。我已经测试了你的建议,它是有效的。然而,我正在做一个类项目,在细节中它提到如下:类Car应该有一个构造函数Car(String model,int year,int price),类NewCar有构造函数NewCar(String model,int year,int price,String color),类UsedCar具有构造函数UsedCar(String model,int year,int price,boolean rusty)我想知道是否有其他方法可以满足描述中提到的要求。看看这个。这很糟糕,但这是可能的。我认为你的任务不应该通过继承来解决。任何汽车都有颜色,任何汽车都可能生锈,所以这些字段必须在汽车类内。那么扩展汽车类就没有意义了。