0

I have array variable declared like this:

public class carshop {
    int numofcars = 0;
    int maxcars = 10;
    ACar[] allCars;
    private CarShop;

public CarShop() { //Car Constructor
    maxcars = maxE;
    allCars = new ACar[maxcars];
    }
}

In my coding example, every time a user adds a new car (via string input), it will increase the numofcars by 1. I have tried changing the array type into a arraylist

ArrayList<ACar> allCars = new ArrayList<ACar>(Arrays.asList());

I changed the allCars = new ACar[maxcars]; line into this: allCars = ACar.add(maxcars);

However now eclipse is giving me errors saying "The method add(int) is undefined for the type ACar".

Can you tell me what I have done wrong? Sorry if I have explained this poorly.

6
  • What is ACar? How is it defined? Commented Jan 6, 2017 at 16:53
  • Where are you using add? Commented Jan 6, 2017 at 16:54
  • 2
    First problem: you've declared a type called carshop but your constructor is for CarShop. Now why are you trying to call add at all in your initialization? And why are you reinitializing the variable when you've already got an initializer? That initializer could be simpler as: List<ACar> allCars = new ArrayList<>(); as well... Commented Jan 6, 2017 at 16:54
  • Java is case-sensitive -- the constructor and class declaration must be named the same... Commented Jan 6, 2017 at 17:01
  • If you want to change the Array in your code to an ArrayList consider that they have some differences: An ArrayList doesn't have a fixed size, it grows as you add Objects to it. So if your logic requires your list to have a maximum length (as your variable maxcars suggests) you should probably choose something different than a Standard ArrayList or be aware that you have to handle yourself that a maximum of 10 cars can get added. Commented Jan 6, 2017 at 17:03

1 Answer 1

2

ACar is an array so it doesn't have the add() method and you need to insert values by doing ACar[x] = value;

If you want to easily convert an array to a List you can just do:

List<ACar> carList = Arrays.asList(allCars);

or for ArrayList specifically:

ArrayList<ACar> carList = new ArrayList<ACar>(Arrays.asList(allCars));

However you should also think about why you have both an array and an ArrayList. You could instead just be doing:

List<ACar> carList = new ArrayList<ACar>(maxCars);

The maxCars variable is optional, you don't need to set the initial size of an ArrayList unless you are trying to optimise the code.

Sign up to request clarification or add additional context in comments.

1 Comment

That doesn't convert to an ArrayList. It converts to a List (which is also named ArrayList but is not a java.util.ArrayList), which is not resizable as an ArrayList is, and is backed by the original array.

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.