Array-List里面的构造函数作为参数之一,如何创建一个新对象? [英] Array-List inside constructor as one of the parameter, how to create a new object?
问题描述
我遇到一个构造函数的问题,该构造函数将arrayList作为参数之一。
i am having problem with a constructor that takes an arrayList as one of the arguments.
public class ItemisedProductLine extends ProductLine{
public static ArrayList<String> serialNumbers;
public ItemisedProductLine(String productCode, double recRetPrice, double salePrice, int quantity, String description, ArrayList<String> SerialNumbers){
super( productCode, recRetPrice, salePrice, quantity, description);
this.serialNumbers = serialNumbers;
}
}
现在在我的班级库我想实例化一个新的ItemisedProductLine并将序列号的arryList传递给构造函数
Now in my class Inventory i want to instantiate a new ItemisedProductLine and pass an arryList of serial number to the constructor
ItemisedProductLine item = new ItemisedProductLine("productCode", 2600, 2490, 2, "descrpition", new ArrayList<String>("1233456", "6789123"));
这在Java中是否可行?它似乎不是一个常见的任务,没有找到任何例子。
Is this possible in Java? It seem to be not a common task to do, did not found any example.
作为替代,我可以使用通用数组而不是Array-List但是我可以因为未知的大小而没有初始化
As alternative i could have used an generic array instead of Array-List but then i can not initialize it because of the unknown size
我希望我不会忘记另一个括号: - )
Let's hope i'm not forgetting another parenthesis :-)
最后一件事是错误是找不到合适的构造函数ArraList<>
Last Thing is the error is "no suitable constructor found ArraList<>"
推荐答案
你可以尝试:
new ArrayList<String>(Arrays.asList("1233456", "6789123"))
这篇关于Array-List里面的构造函数作为参数之一,如何创建一个新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!