4

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<>"

2
  • 1
    As a side note, you don't need to determine the size of an ArrayList upfront. It's just has array in its name because it's backed by an array, and you don't need to really pay much attention to the implementation details. This way you can grow/shrink the list as you please. Commented May 12, 2012 at 0:16
  • 3
    Nothing to do with your question, but just curious. Why are you setting a static field in the constructor liek that? Also, if you can modify or add a new constructor, you can make the last field a vararg and construct the ArrayList from the vararg array. Alternatively, you could just store the array. Commented May 12, 2012 at 0:17

3 Answers 3

23

You could try:

new ArrayList<String>(Arrays.asList("1233456", "6789123"))
Sign up to request clarification or add additional context in comments.

1 Comment

billc.cn: thanks, in fact i don't actually need the field to be static, i never used vararg (i'm a total beginner) but thanks for the option it will be my next thing to look at and learn.
1

@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")

1 Comment

You must not cast the result of Arrays.asList() because the actual type it returns could change in the future making your code inadvertently fail. The method should be a black box to us, and we should only depend on its public interface. Namely, all we can tell is that it returns a List.
0

There is another way using Java 8 Stream API.
You can create a Stream of objects and collect them as a List (or Set or whatever cause 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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.