[英]Incompatible types when passing method in another class
I'm getting incompatible type error when I use these methods from another class, I'm supposed to return an array of all the cars whose average horsepower for a given year is within the range specified. 当我从另一个类中使用这些方法时,出现类型不兼容的错误,应该返回所有给定年份的平均功率在指定范围内的所有汽车的数组。
here is my methods from the other class which returns arraylist: 这是我从另一个类返回arraylist的方法:
public ArrayList<Lamborghini> getCarsFromThisYear(int year){
ArrayList<Lamborghini> fromYear = new ArrayList<Lamborghini>();
for( Lamborghini c : inventory){
if(c.getModelYear() == year){
fromYear.add(c);
}
}
if( fromYear.size() == ZERO){
return new ArrayList<>();
}
return fromYear;
}
public Lamborghini[] getCarsWithHorsepowerRange(double lowHP, double highHP){
int matches = ZERO;
for (Lamborghini c : inventory){
if(c.getHorsepower() >= lowHP && c.getHorsepower() <= highHP){
matches++;
}
}
Lamborghini[] horsepower = new Lamborghini[matches];
int indexInArray = ZERO;
for (Lamborghini c : inventory){
if(c.getHorsepower() >= lowHP && c.getHorsepower() <= highHP){
horsepower[indexInArray] = c;
indexInArray++;
}
}
return horsepower;
}
and here is what im working on, i'm getting error where it calls : c.getCarsFromThisYear(modelYear) and (c.getCarsWithHorsepowerRange(lowHP, highHP) 这是我正在努力的事情,我在它调用的地方出错:c.getCarsFromThisYear(modelYear)和(c.getCarsWithHorsepowerRange(lowHP,highHP)
public LamborghiniCarLot[] getAllCarLotsWithAverageHorsepowerInRangeForYear(int modelYear, double lowHP, double highHP){
int matches = ZERO;
for(LamborghiniCarLot c : carLots){
if (c.getCarsFromThisYear(modelYear)){
if(c.getCarsWithHorsepowerRange(lowHP, highHP)){
matches++;
}
}
}
LamborghiniCarLot[] search = new LamborghiniCarLot[matches];
int index = ZERO;
for(LamborghiniCarLot c : carLots){
search[index] = c;
index++;
}
}
There are number of issues with the following method: 以下方法存在许多问题:
public ArrayList<Lamborghini> getCarsFromThisYear(int year){
ArrayList<Lamborghini> fromYear = new ArrayList<Lamborghini>();
for( Lamborghini c : inventory){
if(c.getModelYear() == year){
fromYear.add(c);
}
}
if( fromYear.size() == ZERO){
return new ArrayList<>();
}
return fromYear;
}
List<Lamborghini>
instead of ArrayList<Lamborghini>
返回List<Lamborghini>
而不是ArrayList<Lamborghini>
ArrayList<>()
to ArrayList<Laborghini>
in your if
statement. 或在if
语句中将ArrayList<>()
更改为ArrayList<Laborghini>
。 Here is a what I would suggest: 这是我的建议:
public List<Lamborghini> getCarsFromThisYear(int year){
List<Lamborghini> fromYear = new ArrayList<Lamborghini>();
for( Lamborghini c : inventory){
if(c.getModelYear() == year){
fromYear.add(c);
}
}
return fromYear;
}
The other issue is that in if (c.getCarsFromThisYear(modelYear))
. 另一个问题是在if (c.getCarsFromThisYear(modelYear))
。 The method returns List<Lamborghini>
but the if statement expecting boolean
value. 该方法返回List<Lamborghini>
但if语句期望boolean
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.