简体   繁体   English

如何在Java中声明ArrayList的ArrayList

[英]how to declare ArrayList of ArrayList in java

I want to declare an ArrayList within another ArrayList . 我要声明一个ArrayList另一个内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. 我声明了ArrayListArrayList并以以下方式成功添加了元素,但是在检索其显示类强制转换异常时。

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. 相反,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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