简体   繁体   English

如何在Java类中声明对象的ArrayList?

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

I'm very unskilled with java and programming, though I am trying to do one of my friend's home exercises! 尽管我正在尝试做我朋友的一项家庭练习,但我对Java和编程的技能很不熟练! :P :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". 问题是,我试图为产品创建一个类模板,然后将它们全部放到来自“产品”类的对象的ArrayList中。 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. 这样做的目的是能够通过简单输入其ID(与ArrayList中的位置相同)来访问所涉及的产品对象。

eg: 例如:

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! 谢谢!

Have a constructor in your System class and then System类中有一个构造函数,然后

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"

If I understand correctly, you can't access the ArrayList outside of your constructor? 如果我理解正确,就无法在构造函数之外访问ArrayList?

You can declare the ArrayList within the class, but then add items within the constructor. 您可以在类中声明ArrayList,然后在构造函数中添加项目。

So... 所以...

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

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM