Array-List inside constructor as one of the parameter, how to create a new object?
Solution 1
You could try:
new ArrayList<String>(Arrays.asList("1233456", "6789123"))
Solution 2
@Edwin's answer is good, but you could also ditch the ArrayList
constructor in favor of a simple cast. Arrays.asList
already creates a new ArrayList
for you.
(ArrayList<String>) Arrays.asList("1233456", "6789123")
fra pet
Updated on November 16, 2020Comments
-
fra pet over 3 years
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; }
}
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"));
Is this possible in Java? It seem to be not a common task to do, did not found any example.
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 :-)
Last Thing is the error is "no suitable constructor found ArraList<>"