The list is an interface, and Listarray is a class.
Listarray inherits and implements the list.
So the list cannot be constructed, but you can create a reference to the list as above, and Listarray can be constructed.
List List; correct list=null;
List List=new list (); Is the wrong usage
List List = new ArrayList (); This sentence creates a ArrayList object that is traced back to list. At this point, it is a list object, some ArrayList have but the list does not have the properties and methods, it can no longer be used.
and ArrayList list=new ArrayList (); Creating an object preserves all the properties of ArrayList.
This is an example:
Import java.util.*;
public class testlist{
public static void Main (string[] args) {
List List = new ArrayList ();
ArrayList ArrayList = new ArrayList ();
List.trimtosize (); Error, there is no such method.
Arraylist.trimtosize (); There is this method in ArrayList.
}
}
Compile and you will know the result.
If this looks like this:
List a=new ArrayList ();
A has all the properties and methods of list and ArrayList, and does not reduce
If the list and ArrayList have the same attributes (such as int i), there is the same method (such as void f ()),
The A.I is called the I in the list
A.F () is called the F () in ArrayList;
---------------------------------------------------------------
The key to the problem:
Why use list List = new ArrayList () instead of ArrayList alist = new ArrayList ()?
The problem is that the list has more than one implementation class, and now you're using ArrayList, maybe someday you need to switch to another implementation class, like LinkedList or vector, and you just change the line:
List List = new LinkedList (); Other code that uses the list place does not need to be changed at all.
Suppose you start with ArrayList alist = new ArrayList (), which you have changed, especially if you have used the ArrayList-specific methods and properties.
Region with List arr = new ArrayList (), definition, industry with Arraylistarr = new ArrayList (), definition; Then there is a special method of ArrayList in the industry.
Another example of this is the following declaration in the method of the class:
private void Domyaction (List list) {}
This method can handle all the classes that implement the list interface, and implement generic functions to some extent.
You can customize your custom class by implementing List,map (or collection) if you feel that Arraylist,hashmap's performance does not meet your needs when developing.
Transferred from: http://www.cnblogs.com/aisiteru/articles/1151874.html
The difference between list and ArrayList in Java