How to declare an ArrayList of objects inside a class in java?

39,179

Solution 1

Have a constructor in your System class and then

class System {

    List<Product> list = new ArrayList<Product>();
    public class System() {

         list.add(new Product(0, "Coca-Cola", 3.00));
         list.add(new Product(1, "Mountain Dew", 2.00));
         list.add(new Product(2, "Mango Smoothie", 4.50));
         list.add(new Product(3,"Orange Juice", 5.00));
         list.add(new Product(4, "Dr. Pepper", 4.50));
         list.add(new Product(5, "Sandwich", 3.00));
         list.add(new Product(6, "Hamburger", 3.50));
         list.add(new Product(7, "Light Sandwich", 4.00));
    }
}

Or,

You can

    class System {

        List<Product> list = new ArrayList<Product>(){

             {
                  add(new Product(0, "Coca-Cola", 3.00));
                  add(new Product(1, "Mountain Dew", 2.00));
                  add(new Product(2, "Mango Smoothie", 4.50));
                  add(new Product(3,"Orange Juice", 5.00));
                  add(new Product(4, "Dr. Pepper", 4.50));
                  add(new Product(5, "Sandwich", 3.00));
                  add(new Product(6, "Hamburger", 3.50));
                  add(new Product(7, "Light Sandwich", 4.00));
             }

       };

    }

But this is not a good way of writing this, You should not hard code the values in your code like this,

And you can not do

System.out.println(list[0].getName); //Prints "Coca-Cola" 

because list is a List,

You can do

System.out.println(list.get(0).getName()); //Prints "Coca-Cola"

Solution 2

If I understand correctly, you can't access the ArrayList outside of your constructor?

You can declare the ArrayList within the class, but then add items within the constructor.

So...

public class System{
     ArrayList<Product> list = new ArrayList<Product>();

     public System(){
          list.add(param1, param2, param3);
          //add stuff here.
     }


}
Share:
39,179
Ophir Neto
Author by

Ophir Neto

Updated on July 09, 2022

Comments

  • Ophir Neto
    Ophir Neto almost 2 years

    I'm very unskilled with java and programming, though I am trying to do one of my friend's home exercises! :P

    The exercise consists in creating a system that shows a list of products available in a store, giving the user the ability to choose what he/she wants, asking for confirmation, then adding to a cart for a later checkout.

    The problem is, I'm trying to make a class template for the products, then putting them all together in an ArrayList of objects from the class "Product". Works fine if I declare said list and add all the objects I want to it inside a function, but I can't declare it inside a class (as if it is a variable of sorts).

    Here's part of the code:

    package system;
    
    class Product {
    
    public int id;
    public String name;
    public double price;
    
    public Product(int startId, String startName, double startPrice){
         startId = id;
         startName = name;
         startPrice = price;
     }
    
    public int getId(){
        return id;
    }
    
    public double getPrice(){
        return price;
    }
    public String getName(){
        return name;
    }
    
    
    }
    //Main Class (The one that has the 'main()' function)
    
    public class System {
    
    ArrayList<Product> list = new ArrayList<Product>();
    list.add(new Product(0, "Coca-Cola", 3.00));
    list.add(new Product(1, "Mountain Dew", 2.00));
    list.add(new Product(2, "Mango Smoothie", 4.50));
    list.add(new Product(3,"Orange Juice", 5.00));
    list.add(new Product(4, "Dr. Pepper", 4.50));
    list.add(new Product(5, "Sandwich", 3.00));
    list.add(new Product(6, "Hamburger", 3.50));
    list.add(new Product(7, "Light Sandwich", 4.00));
    
    
    //Yadda Yadda Yadda
    }
    

    The purpose of doing this is to be able to access the product object in question by simply inputting its id, which is identical to its position in the ArrayList.

    e.g.:

    System.out.println(list[0].getName); //Prints "Coca-Cola"
    

    This strategy makes it easier to access each product, and modularizes it better, allowing me to add changes in the future.

    The only problem is, as I've said, I can't declare it at the beginning of a class. It can only be declared inside a method, and only used in said method, apparently.

    Any suggestions on how I may proceed? I tried researching a bit but couldn't find/understand any suitable answers.

    Also, any tips on how to make this code neatier is very appreciated.

    Thanks!

  • Ophir Neto
    Ophir Neto over 8 years
    Thanks! Also, if I may ask a few more questions:
  • Ophir Neto
    Ophir Neto over 8 years
    Thanks! Netbeans didn't show any warnings, so this should be it!
  • diyoda_
    diyoda_ over 8 years
    @OphirNeto If you are not introducing products to the application, you will have to recompile the project, And you will never be able to localize this application because you are hard coding the values, If you are doing this to understand java, Start with this and learn more on the design patterns. Because the right way of writing the code is more dependent on what you want to achieve
  • Ophir Neto
    Ophir Neto over 8 years
    I see... What you mean is that I basically can't add or subtract products from the code without rewriting it, right? I had in mind that one would not be able to add newer products nor change anything inside them. I wrote them this way for the sake of simplicity, since I would not need extra functionality and dynamism. Thanks for all the tips!
  • Ophir Neto
    Ophir Neto over 8 years
    Also, disconsider my first comment ("Thanks! Also, if I may ask a few more questions: "). I was trying to add a few more info to it, but I took longer than five minutes and couldn't edit it anymore. Consider it as how it was first written: "Thanks! How should I write this code, then?", or something of the sort.