简体   繁体   中英

how to declare ArrayList of ArrayList in java

I want to declare an ArrayList within another ArrayList .

I declared ArrayList of ArrayList and added the elements in the following manner successfully but at the time of retrieval its showing class cast exception.

List<List<UnderDefinedFunction> parentAL=new ArrayList<List<UserDefinedFunction>>();
List<UserdefinedFunction> childAL=new ArrayList<UserDefined>();

childAL.add(userdefinedfunction);
...............
..............
parentAL.add(childAL);

At the time of retrieval it is showing class cast exception.

for(int i=0;i<parentAL.size();i++){
    List<UserDefinedFunction> al=parentAL.get(i);
}

I tried with following code also:

ArrayList<ArrayList<UserDefinedfunction> al=new ArrayList<ArrayList<UserDefinedFunction>>();

but I am getting same problem.

Why is it showing any error any mistake is done any idea ???

Do:

 List<ArrayList<UnderDefinedFunction>> parentAL=new ArrayList<ArrayList<UserDefinedFunction>>();

instead and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM