user2745043
user2745043

Reputation: 199

ArrayList initializing

Hey guys I just have a quick question about initializing an arraylist

This is just a small piece of code I'm doing

public class OrderManager {
    ArrayList<Order>orders = new ArrayList<Order>();
    public OrderManager() {

    }

    public OrderManager(ArrayList<Order> orders) {
        orders = new ArrayList<Order>();
    }

using a variable orders, and then declaring orders = new correct? Or is this going to be two different instances and when I try to add things to it its not going to work?

Or since OrderManager is going to take an arraylist does it even make sense?

I haven't tested it yet, I just started writing this code and have ran into this problem before where I couldn't add to the list properly and believe it was because of a error similar to this just checking to try and get it right to start with.

Upvotes: 1

Views: 346

Answers (3)

Boris the Spider
Boris the Spider

Reputation: 61198

What you are currently doing is assigning a new, empty ArrayList instead of taking the one given.

You should either do this:

public class OrderManager {
    private final List<Order> orders;

    public OrderManager() {
        orders = new ArrayList<Order>();
    }

    public OrderManager(final List<Order> orders) {
        this.orders = orders;
    }

Which will take the reference to the List passed into the constructor. Changes to the List from outside the class will affect the List inside the class.

A more common way is to make a "defensive copy" using the copy constructor

public class OrderManager {
    private final List<Order>orders;

    public OrderManager() {
        orders = new ArrayList<Order>();
    }

    public OrderManager(final List<Order> orders) {
        this.orders = new ArrayList<Order>(orders);
    }

Now the class has a copy of the List passed in so it will be independent of the original List.

Upvotes: 2

PM 77-1
PM 77-1

Reputation: 13344

Your second constructor is wrong.

It should be:

public OrderManager(ArrayList<Order> orders) {
        this.orders = orders;
    }

Constructor is what used to create a new object and initialize its class variables.

When you use new you are calling a class constructor.

There are cases when one constructor can be called from another. It's done when the calling constructor initializes a larger set of variables and uses other constructor to initialize a sub-set (so not to repeat the same code). At such cases you use this with a proper signature.

Upvotes: 0

nachokk
nachokk

Reputation: 14413

public class OrderManager {
    private ArrayList<Order>orders;
    public OrderManager() {
      this(new ArrayList<>());//call constructor
    }

    public OrderManager(ArrayList<Order> orders) {
        this.orders = orders;
    }
  .
  .
   //more methods here for example getters and/or setters for orders
}

This is the proper way. Also consider using List rather than ArrayList cause in future if you want not to be ArrayList and for example be LinkedList you don't have to modify this class. Programming to an interface concept.

So your class would look like:

    public class OrderManager {
        private final List<Order>orders;

        public OrderManager() {
          this(new ArrayList<>());//call constructor or doing nothing is another option
        }

        public OrderManager(List<Order> orders) {
            this.orders = orders;
        }


       public List<Order> getOrders(){
           return orders;
       }    

       public void addOrder(Order order){
            orders.add(order);
       }
  }

Upvotes: 3

Related Questions