我们平时建立数组,例如:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
但是如果想要一个拥有java列表(如ArrayList)的数组,且要表明列表的泛型类型时,照样:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
但这是就出现了Cannot create a generic array of ArrayList<Integer>的错误,如果再改成:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
又会出现“Type safety: The expression of type ArrayList[] needs unchecked conversion to conform to
ArrayList<Integer>[] ”的错误,令人很困惑。
但是又想,如果用第二种方法定义的话,“my“还是不能直接用,要先对每一个数组成员进行初始化后才能用,
所以出现以下:
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
这时就可以对数组里的每一个列表进行泛型规定,保证了强类型检查。(其实不用管前面那个警告了)