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++;
            }
        }
7
  • Can you show the declaration of Lamborghini? Commented Dec 6, 2015 at 5:11
  • 1
    .size(), and you get the index by .get(i) Commented Dec 6, 2015 at 5:11
  • Possible duplicate of How to find the length of an array list? Commented Dec 6, 2015 at 5:13
  • 1
    Lamborghini is list or array? please write complete code . that will help to answer your question. Commented Dec 6, 2015 at 5:16
  • It is list, private ArrayList<Lamborghini> inventory; Commented Dec 6, 2015 at 5:18

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks resueman that was very helpful, thanks for clearly the Array S and ArrayList for me was going around in circles.
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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.