There's a few issues with your code. First off, you're trying to use Lamborghini
to refer to the ArrayList
you declared, when Lamborghini
is just the type of object stored in it. Instead, you should use the variable name, in this case inventory
, to refer to the instance of the ArrayList
that you created.
Secondly, you're confusing Array
s and ArrayList
s, which are different types, and have different ways to access their length and contents. Since you're using an ArrayList
, you need to use the size()
method to get its length, and the .get(int)
method to access the elements.
So change your code to this to correct those errors:
public Lamborghini[] getCarsWithHorsepowerRange(double lowHP, double highHP){
int horsePower = 0;
for(int i = 0; i < inventory.size(); i++){
if(inventory.get(i) != null){
if((inventory.get(i).getHorsePower() >= lowHP) &&
(inventory.get(i).getHorsePower() <= highHP)){
horsePower++;
}
}
}
}
This will still have a problem, since it has a return type of Lamborghini[]
(an array of Lamborghini
), which is never returned. I don't know what the intent is, but if you don't need the return value, you can just change the type to void
. Or you could change the type to ArrayList<Lamborghini>
and return the inventory
object. Otherwise, you'll need to create a new array and populate it:
Lamborghini[] result = new Lamborghini[inventory.size()];
for(int i = 0; i < inventory.size(); i++){
result[i] = inventory.get(i);
}
return result;