I have a constructor that accepts an ArrayList<String>
, but wants to call super
expecting a String[]
array.
I have tried the following, but it results in a class exception, [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;
public cool(ArrayList<String> s) {
super((String[]) s.toArray());
}
I'd like to be able to pass cool
an ArrayList<String>
Thanks
EDIT: I have tried the recent suggestion to use
super(s.toArray(new String[s.size()]));
but now I get the following exception:
entity must have a no-arg constructor.; nested exception is java.lang.IllegalArgumentException: : entity must have a no-arg constructor.
Try this:
super(s.toArray(new String[s.size()]));
The above is the type-safe way to convert an ArrayList
into an array, it's not a really cast, just a conversion.
Regarding the new error reported - you have to declare a no-arg constructor in the entity mentioned in the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With