Java – how to create a new object with the constructor in array list as one of the parameters?
•
Java
I have a problem with a constructor that takes ArrayList as one of its parameters
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; }
}
Now in my class library, I want to instantiate a new itemisedproductline and pass the arrylist of the serial number to the constructor
ItemisedProductLine item = new ItemisedProductLine("productCode",2600,2490,2,"descrpition",new ArrayList<String>("1233456","6789123"));
Is this possible in Java? This does not seem to be a common task and no examples have been found
As an alternative, I can use a generic array instead of array list, but I can't initialize it because of the unknown size
Let's hope I don't forget another parenthesis: -)
The last thing is that the error is "no suitable constructor arralist < >"
Solution
You can try:
new ArrayList<String>(Arrays.asList("1233456","6789123"))
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码