1

Have method below that returns an array of all Lamborghini objects that have horsepower in the range passed as parameter. When I try to compile error at Lamborghini.length cannot find symbol - variable length. Isn't length part of ArrayList?

public Lamborghini[] getCarsWithHorsepowerRange(double lowHP, double highHP)
{

    int horsePower = 0;

    for(int i = 0; i < Lamborghini.length; i++)
    {
        if(Lamborghini[i] != null)
        {
            if((Lamborghini[i].getHorsePower() >= lowHP) &&
            ((Lamborghini[i].getHorsePower() <= highHP)))
            {
                horsePower++;
            }
        }
m4n0
  • 29,823
  • 27
  • 76
  • 89
TOD
  • 91
  • 6

2 Answers2

2

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 Arrays and ArrayLists, 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;
resueman
  • 10,572
  • 10
  • 31
  • 45
  • thanks resueman that was very helpful, thanks for clearly the Array S and ArrayList for me was going around in circles. – TOD Dec 06 '15 at 05:39
0

lets say lamborghiniList is the list of Lamborghini object.. initialize it in constructor or by a setter method you can try following code...

public Lamborghini[] getCarsWithHorsepowerRange(double lowHP, double highHP) {

    List<Lamborghini> desiredObjects = new ArrayList<Lamborghini>();

    for (int i = 0; i < lamborghiniList.size(); i++) {
        if (lamborghiniList.get(i) != null) {
            if ((lamborghiniList.get(i).getHorsePower() >= lowHP) &&
                    ((lamborghiniList.get(i).getHorsePower() <= highHP))) {
                desiredObjects.add(lamborghiniList.get(i));

            }
        }
    }
    return desiredObjects.toArray();
}
Mukesh Kumar
  • 783
  • 1
  • 9
  • 24