我在这里实现了ArrayListpublic class JsonList<T> extends ArrayList<T>{//extended functions}创建ArrayList时,您可以执行List<?> arrayList = new ArrayList<>(alreadyCreatedList);我希望能够通过JsonList做到这一点。但目前,JsonList仅具有默认构造函数JsonList ()。我试过像这样复制ArrayList构造函数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; } }当创建一个JsonList实例时JsonList<?> jsonList = new JsonList<>(alreadyCreatedList);但是,不会保存元素。它返回一个empty array。此外,我无法再创建一个空实例JsonList<?> jsonList = new JsonList<>();解:我不知道为什么我没有想到,但是对于那些在那里的人来说public JsonList(Collection<? extends E> c) { super(c); // <-- invoke the appropriate super constructor}您只需要super(c)。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 将第一行设为super(c);,编译器将插入super(),该行将不会调用您要的行(使用Collection<? extends E> c)。public JsonList(Collection<? extends E> c) { super(c); // <-- invoke the appropriate super constructor elementData = c.toArray(); // ...}关于java - 扩展ArrayList并使用/创建类似的构造函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48533296/ (adsbygoogle = window.adsbygoogle || []).push({});
10-09 01:30