'How to use add() in ArrayList class in Java

I have a question about constructing an ArrayList, I have had a Car Class, now I need to make an ArrayList to put content in, I named it as Race, please see code below:

import java.util.ArrayList;


public class Race {

private ArrayList<Car>cars;

public Race(){
cars=new ArrayList<Car>();
}

Now I need to have a method for add content to the ArrayList, there are two ways to write that I am confused with:

First one

public void addCars(){

Car Toyota=new Car("Toyota",1.0,1.0,2.0,2.0);
Car Honda=new Car("Honda",1.0,2.0,1.0,2.0);
Car Mazda=new Car("Mazda",1.0,3.0,2.0,3.0);
Car Suzuki=new Car("Suzuki",1.0,4.0,4.0,2.0);

cars.add(Toyota);
cars.add(Honda);
cars.add(Mazda);
cars.add(Suzuki);

    }

Second one

public void addCars(Object nm,String n, double s, double p, double a, double b){

Car name=new Car(n,s,p,a,b);
cars.add(name);
    }

Both ways have no mistake reported when I coding, but I am not sure which one is correct, or maybe neither is correct, please help, cheers!

UPDATE:

    public void addCars(Car car){
cars.add(car);

   } 

This is what I used finally, then I created a new class called Test that using main method to add cars individually, but there is mistake in the last line:

    public class Test {

public static void main(String[] args) {
    Car Toyota=new Car("Toyota",1.0,1.0,2.0,2.0);
    **cars.addCars(Toyota);**

I have no idea how to fix it, please help!



Solution 1:[1]

It depends on what you want to achieve. In general it is better to provide APIs and use them from the outside. In your case, however, I think your "addCars" function in used to populate your ArrayList(you should call it something like that) - which means to add predefined values to a Collection. In that case, use the first one.

So, both of your methods are correct (though you should call the first populateCars and the second addCar) and should work, but you need to use them depending on your circumstance.

Also, if you want to provide an API for adding Cars, let the user get a Car object himself, rather than constructing one in the add method. (You might want to change it before adding it, you never know)

So I suggest using either of the methods, but changing the first to:

public void populateCars(){
    Car Toyota=new Car("Toyota",1.0,1.0,2.0,2.0);
    Car Honda=new Car("Honda",1.0,2.0,1.0,2.0);
    Car Mazda=new Car("Mazda",1.0,3.0,2.0,3.0);
    Car Suzuki=new Car("Suzuki",1.0,4.0,4.0,2.0);

    cars.add(Toyota);
    cars.add(Honda);
    cars.add(Mazda);
    cars.add(Suzuki);
}

or the second to:

public void addCar(Car car){
    cars.add(car);
}

Solution 2:[2]

Both the ways you mentioned are correct but you should modify the method addCars() to take an argument of type Car .

For example , change the method signature to :

public void addCars(Car car){
     cars.add(car);
}

And just pass a new car like this :

addCars(new Car("Toyota",1.0,1.0,2.0,2.0));

Solution 3:[3]

The only difference is that in first case you have hard coded elements in the list and in the second approach you add them dynamically. What to use depends on your need. I suppose you will use the arraylist later on to get elements from it, do sth with them etc. Then you will use getter method on the arraylist, iterate through it and so on... This will work on both approaches you choose.

Solution 4:[4]

Both answers are correct. The first method uses a traditional approach to creating objects. The second method is sometimes called a Factory method. Using a factory method is useful when the construction of an object is externally complicated or stamping out multiple objects.

So the answer is: The first approach is correct for objects that are reasonably simple and safe to construct. The second (factory) pattern is better for complex objects.

Solution 5:[5]

@pei wang nice question .. Second apporach is gud as ur not hardcoding values .. You should go little bit forward .. please Use DTO design pattern in second approach use setters and getters

Solution 6:[6]

Add method expects object type reference for the add method if you pass any reference variable other than object type reference variable then those reference variable upcasted to object type reference.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Java World
Solution 3 Hrvoje G.
Solution 4 Nathaniel Johnson
Solution 5 Luckyman Nevermore
Solution 6