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>.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章