关于List和ArrayList的使用

xueqilinqqq 2015-05-05 09:50:48
小弟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,可能有看起来很蠢的地方想不通,见笑了。
...全文
296 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
飏飏一蝶 2015-05-05
  • 打赏
  • 举报
回复
引用 4 楼 xueqilinqqq 的回复:
[quote=引用 3 楼 qq118194716 的回复:] List<List<Integer>> result= new ArrayList<List<Integer>>(); List只是泛型接口,里面的也并不是ArrayList,而只是元素是List<Integer>类型的引用而已,被初始化为null 你赋值时它才知道自己的每个元素引用了一个什么样的List<Integer>的子类
可能我的表述有问题,我的意思是 外层的List我们显式地告诉了它 要引用的是ArrayList型的子类; 但是对于里层的List<Integer>,并没有明确地声明,照您所说,是等到我赋值的时候程序才知道子类的类型是ArrayList,那举个例子: List<List<Integer>> result= new ArrayList<List<Integer>>(); 可以理解为我在里层的List可以存储其他类型比如LinkedList的子类元素吗?[/quote] Score!
xueqilinqqq 2015-05-05
  • 打赏
  • 举报
回复
引用 3 楼 qq118194716 的回复:
List<List<Integer>> result= new ArrayList<List<Integer>>(); List只是泛型接口,里面的也并不是ArrayList,而只是元素是List<Integer>类型的引用而已,被初始化为null 你赋值时它才知道自己的每个元素引用了一个什么样的List<Integer>的子类
可能我的表述有问题,我的意思是 外层的List我们显式地告诉了它 要引用的是ArrayList型的子类; 但是对于里层的List<Integer>,并没有明确地声明,照您所说,是等到我赋值的时候程序才知道子类的类型是ArrayList,那举个例子: List<List<Integer>> result= new ArrayList<List<Integer>>(); 可以理解为我在里层的List可以存储其他类型比如LinkedList的子类元素吗?
飏飏一蝶 2015-05-05
  • 打赏
  • 举报
回复
List<List<Integer>> result= new ArrayList<List<Integer>>(); List只是泛型接口,里面的也并不是ArrayList,而只是元素是List<Integer>类型的引用而已,被初始化为null 你赋值时它才知道自己的每个元素引用了一个什么样的List<Integer>的子类
xueqilinqqq 2015-05-05
  • 打赏
  • 举报
回复
引用 1 楼 xueqilinqqq 的回复:
[quote=引用 楼主 xueqilinqqq 的回复:] 小弟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,可能有看起来很蠢的地方想不通,见笑了。
又看了一遍,发现Integer拼错了,汗,第一条其实是可执行的。 不过还是想请教一下,如果有大牛能讲解一下这方面的基础就好了。因为虽然成功了都是试出来的,脑子里对这样的调用还不是100%吃透。[/quote] 就比如List<List<Integer>> result= new ArrayList<List<Integer>>(); 只是声明了外层的List是用ArrayList来实现,那对于里面的List并没有声明呀,java为什么会知道里面的也是ArrayList呢?求解答。
xueqilinqqq 2015-05-05
  • 打赏
  • 举报
回复
引用 楼主 xueqilinqqq 的回复:
小弟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,可能有看起来很蠢的地方想不通,见笑了。
又看了一遍,发现Integer拼错了,汗,第一条其实是可执行的。 不过还是想请教一下,如果有大牛能讲解一下这方面的基础就好了。因为虽然成功了都是试出来的,脑子里对这样的调用还不是100%吃透。

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧