I implemented ArrayList here
public class JsonList<T> extends ArrayList<T>{
//extended functions
}
When creating an ArrayList, you can do something List<?> arrayList = new ArrayList<>(alreadyCreatedList);
I want to be able to do this with my JsonList. But currently, JsonList only has the default constructor JsonList<>(). I tried copying the ArrayList constructor like this
public JsonList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// defend against c.toArray (incorrectly) not returning Object[]
// (see e.g. https://bugs.openjdk.java.net/browse/JDK-6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
And when creating a JsonList instance I do
JsonList<?> jsonList = new JsonList<>(alreadyCreatedList);
However, the elements are not saved. It returns an empty array. In addition, I can no longer create an empty instance JsonList<?> jsonList = new JsonList<>();
Solution: I don't know why I didn't think of it but for those out there here it is
public JsonList(Collection<? extends E> c) {
super(c); // <-- invoke the appropriate super constructor
}
super(c) is all you need.