Java Learning Notes-Problem ArrayList cannot be converted to List

List is an interface in Java,so you can not instantiate the type List<?>,the collection class ArrayList  extends "AbstractList" and implements the "List" interface.So we should be able to cast a ArrayList Object to a List Object,like this:

List<Integer> lst=new ArrayList<Integer>();

The reason why we can cast a ArrayList to a List is that ArrayList implements to interface of List,So the ArrayList can be cast to a List object,but not the other way round.we can think this problem in a simple way,List as an interface has less fileds than ArrayList,so we can call everything in List,and all of those have been inplemented in ArrayList.So if we cast ArrayList to a List,it will not make any mistakes.But,if we call a method in ArrayList,we can not promit that there would be no errors,because there may not have such method which is called in ArrayList.In a word,we can cast a larger object to a smaller object,but not the other way round.

  But I have met some problem when today I suppose to solove a problem in leetcode,we need to cast a generics ArrayList to a List,which likes this:

List<List<Integer>> lst=new ArrayList<ArrayList<Integer>>();

if you write this code in eclipse or other IDE of Java,you may meet a error like this : Type mismatch: cannot convert from ArrayList<ArrayList<Integer>> to List<List<Integer>>

It says can not convert ArrayList<ArrayList<Integer>> to List<List<Integer>>,which has diffused me a second.As I said upper,we can convert ArrayList to a List,because,ArrayList implements the interface List,and why there makes a failures?To solove this problem I have searched in BaiDu,and found a anser which can perfectly explains this problem.And this problem is also an frequently encountered problem about Generics, Inheritance, and Subtypes.If you want to learn this problem more carefully,you can access the lower website,and I will explain it by my own words.

Generics, Inheritance, and Subtypes:https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html

1.You can convert a subclass to it's superclass,or convert a subclass to it's interface.

as the picture show,List is a subclass of Collection and ArrayList is a subclass of List,so we can convert ArrayList to List or Convert ArrayList to Collection.Of course,Object class is a base class of any object,so we can convert any class to Object class.

2.You can not convert a generic class to another generic class.

For example: public void boxTest(Box<Number> n) { /* ... */ }

We can not send Box<Integer> or Box<Double> to this method,although both Integer and Double are subclass of Number.We can understand this problem more clearly by lower's picture.

As is shown,although Interger is a subclass of Number,but Box<Integer> isn't a subclass of Box<Number>,so the convert will produce errors.

Returns back to the beginning problem,We can change our code into:

List<List<Integer>> lst=ArrayList<List<Integer>>

And now,we know that our mistake is that ArrayList<Integer> isn't a subclass of List<Integer>.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值