1

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!

4
  • 1
    These two approaches aren't the same.. The first one adds a predetermined set of values to the list, the second one provides a method that can add a single car to the list. You'll have to elaborate on your use case: will cars be added outside the class or not? Commented Oct 6, 2013 at 12:48
  • 2
    Duplicating the Car's constructor arguments in addCars() is usually not a correct approach. Instead define addCars(List<Car> cars) or addCar(Car car) and construct the car to be added outside of the function. Commented Oct 6, 2013 at 12:50
  • change to cars.addCars(Toyota) Commented Oct 6, 2013 at 13:15
  • Hi Udy, I did, but still error, it shows" cars cannot be resolved" Commented Oct 6, 2013 at 13:17

6 Answers 6

3

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

6 Comments

hi Xandaros, thank you for your advice, when I write a method, can I put actual value into the method body? I thought I can only pass the parameters to the method body, the actual value should be used in main class
I'm not sure what exactly you mean. You can construct the car outside and pass it as a parameter. Or you can construct a bunch of objects inside the method to populate the ArrayList. There is no restriction as to where you can construct objects.
Thank you so much Xandaros, please see my update for another question, thanks for your help!
I would add a comment to your question, but I don't have enough rep, so... You are trying to add cars to nothing. cars does not exist in that context. If you added a method getCar to a class, you need to invoke the method on an object of that class. So first get your object of the class that contains the cars and call addCar on that.
So I need to have a getter method in the cars class and invoke it in the test class to add cars, right?
|
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));

2 Comments

addCars(new Car("Toyota",1.0,1.0,2.0,2.0)) is just showing a way to add a new Car object . You can add as many cars as you want . That's why the name of method is addCars and not addCar
I don't follow you. You are adding one car in the method. Therefore addCar. If the method was public void addCars(Car... car) I would agree with you.
1

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.

Comments

1

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.

Comments

0

@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

Comments

0

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.

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.