Array-List inside the constructor as one of the parameters, how to create a new object?

I have a problem with a constructor that takes an array of List 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 Inventory class I want to instantiate a new ItemizedProductLine and pass the serial number arryList 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, but found no example.

Alternatively, I could use a shared array instead of an Array-List, but then I cannot initialize it due to an unknown size

I hope I do not forget another bracket :-)

The last thing is the error: "No suitable ArraList constructor was found <>"

+3
source share
3 answers

You can try:

new ArrayList<String>(Arrays.asList("1233456", "6789123"))
+23

@Edwin , ArrayList . Arrays.asList new ArrayList.

(ArrayList<String>) Arrays.asList("1233456", "6789123")

+1

There is another way to use the Java 8 Stream API . You can create a stream of objects and assemble them as a List (either Install or any reason why you can build your own collector ).

Stream.of(
        new MyClassConstructor("supplierSerialNo", "supplierSerialNo", String.class),
        new MyClassConstructor("title", "title", String.class),
        new MyClassConstructor("kind", "kind", String.class))
.collect(Collectors.toList())
0
source

All Articles