关于List和ArrayList的使用
小弟java菜鸟,正在同时学习算法和语法中。做leetcode遇到了pascal三角那道题。在使用List和ArrayList的时候遇到语法问题,特来请教。
假定已经引入util包。
如果直接声明如:
ArrayList result = new ArrayList();
是无语法错误的。那如果想声明List类型的实例呢?(因为Leetcode需要返回List<List<Integer>>)
查API文档得知,List是interface而ArrayList是class,那么如果想声明一个List的实例,我想可以用:
List result = new ArrayList();
这句话来实现。我这样声明,没有语法错误。
那pascal三角需要声明一个二维的List,该如何声明呢?
我试过
List<List<Integer>> result= new ArrayList<List<interger>>();
List<List<Integer>> result= new ArrayList<List<interger>()>();
List<List<Integer>> result= new ArrayList<ArrayList<interger>>();
List<List<Integer>> result= new ArrayList<ArrayList<interger>()>();
都报错了。
**************************
我想请问大牛们关于List和ArrayList的知识,他们之间的关系,如何用ArrayList去声明List? 尤其是二维的List,为什么我会出现语法错误呢(按照推理应该没错呀)?初入java,可能有看起来很蠢的地方想不通,见笑了。