带ArrayList的Java泛型<;?扩展A>;添加元素

带ArrayList的Java泛型<;?扩展A>;添加元素,java,generics,arraylist,Java,Generics,Arraylist,我有A、B、C和D,其中B扩展A,C扩展A和D扩展A 我有以下ArrayLists,每个都有一些元素: ArrayList<B> b; ArrayList<? extends A> mix = b; 但是IDE不允许我这样做,它说: 无法通过调用方法将anElementOfTypeC转换为CAP#1 CAP#1是新类型变量的转换:CAP#1扩展了 捕获?延伸 我是否使用了无法在使用的集合中添加元素?扩展 ArrayList您可以声明: ArrayList<A>

我有
A
B
C
D
,其中
B
扩展
A
C
扩展
A
D
扩展
A

我有以下
ArrayList
s,每个都有一些元素:

ArrayList<B> b;
ArrayList<? extends A> mix = b;
但是IDE不允许我这样做,它说:

无法通过调用方法将anElementOfTypeC转换为CAP#1 CAP#1是新类型变量的转换:CAP#1扩展了 捕获?延伸


我是否使用了
无法在使用
的集合中添加元素?扩展


ArrayList您可以声明:

ArrayList<A> mix = new ArrayList<A>();
ArrayList mix=new ArrayList();
您可以将类A的任何元素或其任何子类添加到这样的列表中。只是当您从该列表中获取时,您将只能调用
A
上可用的方法


请注意,
A
不仅仅是一个“成熟的”类:它可以是一个抽象类,甚至是一个接口。
ArrayList您可以创建超类类型的数组列表。所以你可以这样做

ArrayList<A> arrayList=new ArrayList<A>();
ArrayList ArrayList=new ArrayList();

我是否使用了
查看此示例并根据您的要求进行选择

package com.generic;

import java.util.ArrayList;

public class SuperExtendTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

    }
    public void test() throws Exception {
        ArrayList<Parent> parents=new ArrayList<>();
        parents.add(new Parent());
        parents.add(new Child());
        //parents.add(new GrandParent()); Not allowed

        ArrayList<Parent> p=new ArrayList<>();
        ArrayList<Child> c=new ArrayList<>();
        ArrayList<GrandParent> gp=new ArrayList<>();

        testExtendP(p);
        //testExtendP(c); Not allowed only super type allowed no child
        testExtendP(gp);

        testExtends(c);
        testExtends(p);
        //testExtends(gp); Not allowed because GrantParent does not extends Parent
    }

    /**
     * This Method allowed get operations for Parent 
     * No add 
     * 
     */
    public void testExtends(ArrayList<? extends Parent> list) throws Exception {
    /*  list.add(new Child());
        list.add(new Parent());
        list.add(new GrandParent());
        // can not add
        */
        Child c=(Child) list.get(0);
        Parent parent=list.get(0);
        GrandParent gp=list.get(0);
        /**
         * Unsafe collection way
         */
        ArrayList list2=new ArrayList();
        list.addAll(list2);
    }

    /**
     * This Method allowed add operations for Parent and it's sub types and on get it gives Object type 
     * 
     */
    public void testExtendP(ArrayList<? super Parent> list) throws Exception {
        list.add(new Child());
        list.add(new Parent());
        //list.add(new GrandParent());
        /*can not add because GrandParent can not be converted to Parent type
         * 
         * The method add(capture#3-of ? super Parent) in the type ArrayList<capture#3-of ? super Parent> is not applicable for the arguments (GrandParent)
         * 
         */

        Child c=(Child) list.get(0);
        Parent parent=(Parent) list.get(0);
        GrandParent gp=(GrandParent) list.get(0);
        Object obj=list.get(0);

        /**
         * Unsafe collection way
         */
        ArrayList list2=new ArrayList();
        list.addAll(list2);
    }

    /**
     * This Method allowed all operations for Parent and it's sub types 
     * 
     */
    public void testDirect(ArrayList<Parent> list) throws Exception {
        list.add(new Child());
        list.add(new Parent());
        //list.add(new GrandParent()); 
        /*
         * Can not add GrandParent (Normal generic rule)
         */
    }

}
class GrandParent{

}

class Parent extends GrandParent{

}
class Child extends Parent{

}
package.com.generic;
导入java.util.ArrayList;
公共类超级扩展测试{
/**
*@param args
*/
公共静态void main(字符串[]args){
}
public void test()引发异常{
ArrayList父项=新的ArrayList();
添加(新父项());
parents.add(newchild());
//parents.add(新祖父母());不允许
ArrayList p=新的ArrayList();
ArrayList c=新的ArrayList();
ArrayList gp=新的ArrayList();
testExtendP(p);
//testExtendP(c);不允许仅允许超级类型不允许子类型
testExtendP(gp);
测试范围(c);
测试范围(p);
//testExtends(gp);不允许,因为GrantParent不扩展父级
}
/**
*此方法允许对父级执行get操作
*不加
* 
*/

public void testextens(ArrayList)从您的代码摘录中,
mix
实际上是一个
ArrayList
,在您的研究过程中-如果您做了一些,您是否遇到了这个问题-?无法添加任何内容有点太严格。我们可以添加
null
,也可以添加
list.get(0)
回到列表。我知道我选的太多了。;)但是你可以声明
Class@RohitJain:否,无法添加
列表。获取(0)
返回到list@JBNizet:我知道你在说什么,但这不是评论的主题。那是
List
,而不是
List@JBNizet:我知道代码是怎么说的。我不否认元素来自
列表这一事实
List<? extends A> listA1 = new ArrayList<A>();
List<? extends A> listB1 = new ArrayList<B>();
List<? extends A> listC1 = new ArrayList<C>();
List<? extends A> listD1 = new ArrayList<D>();
listB1.add(new C());
List<A> list = new ArrayList<A>();
package com.generic;

import java.util.ArrayList;

public class SuperExtendTest {

    /**
     * @param args
     */
    public static void main(String[] args) {

    }
    public void test() throws Exception {
        ArrayList<Parent> parents=new ArrayList<>();
        parents.add(new Parent());
        parents.add(new Child());
        //parents.add(new GrandParent()); Not allowed

        ArrayList<Parent> p=new ArrayList<>();
        ArrayList<Child> c=new ArrayList<>();
        ArrayList<GrandParent> gp=new ArrayList<>();

        testExtendP(p);
        //testExtendP(c); Not allowed only super type allowed no child
        testExtendP(gp);

        testExtends(c);
        testExtends(p);
        //testExtends(gp); Not allowed because GrantParent does not extends Parent
    }

    /**
     * This Method allowed get operations for Parent 
     * No add 
     * 
     */
    public void testExtends(ArrayList<? extends Parent> list) throws Exception {
    /*  list.add(new Child());
        list.add(new Parent());
        list.add(new GrandParent());
        // can not add
        */
        Child c=(Child) list.get(0);
        Parent parent=list.get(0);
        GrandParent gp=list.get(0);
        /**
         * Unsafe collection way
         */
        ArrayList list2=new ArrayList();
        list.addAll(list2);
    }

    /**
     * This Method allowed add operations for Parent and it's sub types and on get it gives Object type 
     * 
     */
    public void testExtendP(ArrayList<? super Parent> list) throws Exception {
        list.add(new Child());
        list.add(new Parent());
        //list.add(new GrandParent());
        /*can not add because GrandParent can not be converted to Parent type
         * 
         * The method add(capture#3-of ? super Parent) in the type ArrayList<capture#3-of ? super Parent> is not applicable for the arguments (GrandParent)
         * 
         */

        Child c=(Child) list.get(0);
        Parent parent=(Parent) list.get(0);
        GrandParent gp=(GrandParent) list.get(0);
        Object obj=list.get(0);

        /**
         * Unsafe collection way
         */
        ArrayList list2=new ArrayList();
        list.addAll(list2);
    }

    /**
     * This Method allowed all operations for Parent and it's sub types 
     * 
     */
    public void testDirect(ArrayList<Parent> list) throws Exception {
        list.add(new Child());
        list.add(new Parent());
        //list.add(new GrandParent()); 
        /*
         * Can not add GrandParent (Normal generic rule)
         */
    }

}
class GrandParent{

}

class Parent extends GrandParent{

}
class Child extends Parent{

}