如何在java中的类中声明对象的ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32418252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare an ArrayList of objects inside a class in java?
提问by Ophir Neto
I'm very unskilled with java and programming, though I am trying to do one of my friend's home exercises! :P
我对Java和编程非常不熟练,尽管我正在尝试做我朋友的家庭练习之一!: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).
问题是,我正在尝试为产品制作一个类模板,然后将它们全部放在来自“产品”类的对象的 ArrayList 中。如果我声明所述列表并将我想要的所有对象添加到函数中,则工作正常,但我不能在类中声明它(就像它是某种变量一样)。
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 来访问有问题的产品对象,该 id 与其在 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!
谢谢!
采纳答案by diyoda_
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"
回答by Kai Mou
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.
}
}