The difference between list and ArrayList in Java (Personal insights added)

Source: Internet
Author: User

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

The first time to read this article, is in the CSDN blog to see, the author of the original intention is still good, but there is a wrong place and not very clear

White Place. So I would like to see the original, the waste of a half-day Kungfu finally found, the original is still the same error and I do not understand the place, I also put the original

The address of the article is affixed to the top. March Learning Java Collection Framework This part, these days take out to tidy up to want to go deeper, therefore also looked a lot of off

In the Java Collection framework of the article, this article I have a little bit of experience, I just changed some I think the wrong place and not fluent.


The original text changed:

The list is an interface, and ArrayList is an implementation class for the list interface.

The ArrayList class inherits and implements the list interface.

Therefore, the list interface cannot be constructed, that is, we cannot create an instance object, but we can create a reference to the list interface as follows

Reference to its own object, and the instance object of the ArrayList implementation class is acting as an object reference to the list interface.


Personal Insights:

If you have learned OOP, the above part is not difficult to understand, this is an object-oriented point of knowledge, object-oriented is the most important polymorphism,

We all know that interfaces and abstractions cannot be instantiated, but they can create a pointer to their own object reference, and their implementation class or subclass acts as a

Kind of role, I think this is the advantage of polymorphism in object-oriented programming. The previous days when learning the UML modeling language and Java design patterns, deeply

Realizing the benefits of object-oriented programming, it's not surprising that Java is an object-oriented programming language in Java's collection framework.

The above understanding may be a bit difficult, but we can find a specific example, it will be easier to understand. We define an abstract class of animals

Animal, then define a dog class that inherits from the Animal base class, and see the following code to understand that abstract classes and interfaces cannot be instantiated:

<span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (string[] args) {Animal A1 = new Animal ();//Compilation error Animal a2 = new Dog ();}} Abstract class animal{//Animal name string name;//Animal call public void shout () {System.out.println ("cry ...");} Class Dog extends animal{//Dog class unique method public void guard () {System.out.println ("The dog has a unique ability to janitor!") ");}} </span>
Compilation Result:



List list;//is correct, list = null;

List List = new list ();//is the wrong use

List List = new ArrayList (); This sentence creates an object of the ArrayList implementation class and then traces it back to the list interface. At this point it is a list of

As it is, some ArrayList classes have properties and methods that the list interface does not have, and it can no longer be used. and ArrayList List=newarraylist ();

Creating an object preserves all the properties and methods of ArrayList.


Personal Insights

We continue with the above example, if we create an object reference to an abstract class, then this object can only invoke its own non-abstract method, the following

is the shout () method, cannot call the unique method that inherits its subclass, and the following is the Guard () method that cannot be called by A1 to continue testing the code :

<span style= "FONT-SIZE:18PX;" >public class Test{public static void Main (string[] args) {Animal A1 = new Dog (); A1.shout ();//compile Via//a1.guard ();//Compile error} }abstract class animal{//Animal name string name;//Animal call public void shout () {System.out.println ("cry ...");} Class Dog extends animal{//Dog class unique method public void guard () {System.out.println ("The dog has a unique ability to janitor!") ");}} </span>

Compilation Result:


If we use dog D1 = new Dog (), then D1 can invoke all the properties and methods of the abstract class and subclasses, which are no longer tested.


This is an example:

<span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class demo{public static void Main (string[] args) {List List = new ArrayList ();                 ArrayList ArrayList = new ArrayList ();                List.trimtosize ();//error, no this method.                arraylist.trimtosize (); There is this method in//arraylist. }}</span>

Compile and you will know the result.


Personal Insights

I just listed in the previous article the list interface in the Java API and the methods of the ArrayList implementation class, you can see that the list interface does not have

TrimToSize () method, but this method is available in its implementation class ArrayList. So the result of the compilation is:


If it looks like this:

List a=new ArrayList ();

A owns all the properties and methods of the list, and does not own the unique properties and methods of its implementation class ArrayList.

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 interface has multiple implementation classes, and now you're using ArrayList, and maybe someday you need to switch to another implementation class, such as

LinkedList or vectors and so on, you just have to change this line: List List = new LinkedList (); Other code roots that use the list place

There is no need to change this.

Suppose you start with ArrayList alist = new ArrayList (), which you have changed, especially if you used the ArrayList implementation class-specific method

and properties.


Personal Insights

The above note, I was looking at the design pattern only to realize that the principle and concept of design pattern is strong, only to learn the face to the

Like principles and design patterns, it is no longer difficult to understand the above, and the benefits are for code maintainability, reusability, extensibility, and

Activity, and this is in line with the principle of the Richter substitution and the opening and closing principle. It seems that learning design patterns benefit greatly.



Region with List arr = new ArrayList (); industry with ArrayList arr = new ArrayList (); the definition, the industry uses ArrayList

A special method.

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.

If you feel that Arraylist,hashmap performance does not meet your needs when developing, you can customize it by implementing List,map (or collection).

Your custom class.


Personal Insights

Because the list is an interface, it's extensibility is good, this is the biggest change in object-oriented programming, and it's the core, where I'm feeling

In a word, is the Java Collection framework of learning the most difficult to show you learn the Java language of the program is much deeper, it seems that the extent of my previous study is to stay in the

Door level, these days can be a good refresher to re-understand the Java collection framework.

May understand is not very deep, to the original language understanding also these, the wrong place oneself will promptly correct. Originally wanted to draw a description, but since

UML Learning is not how, there is no serious painting, later or a lot of learning to draw.




The difference between list and ArrayList in Java (Personal insights added)

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.