Android Development--list and ArrayList differences

Source: Internet
Author: User

The list is an interface, and ArrayList is a class.
ArrayList inherits and implements the list.
So the list cannot be constructed, but you can create a reference to the list as above, and ArrayList 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.

Reprinted from: http://www.cnblogs.com/aisiteru/articles/1151874.html

Android Development--list and ArrayList differences

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.