Home > Java > JavaBase > The difference between list and arraylist in java

The difference between list and arraylist in java

王林
Release: 2019-11-18 13:17:23
Original
2232 people have browsed it

The difference between list and arraylist in java

Difference:

List is an interface, and ArrayList is an implementation class of the List interface. The ArrayList class inherits and implements the List interface.

Therefore, the List interface cannot be constructed, that is to say, we cannot create instance objects, but we can create an object reference pointing to ourselves for the List interface as follows, and the instance object of the ArrayList implementation class is This acts as an object reference to the List interface.

Animals.java
public  abstract class Animals {
    //动物名字
    String name;
    //动物叫声
    public  void shout(){
        System.out.println("叫声----");
    }
}

Dog.java
public class Dog extends Animals {
    //狗类独有的方法
    public void guard(){
        System.out.println("狗独有的看门本领");
    }
}

Test.java
public class Test {

    public static void main(String[] args) throws ClassNotFoundException {
        Animals a1 = new Animals();
        Animals a2 = new Dog();
    }
}
Copy after login

The difference between list and arraylist in java

List list;//正确,list = null;
List list = new List();//是错误的用法
Copy after login

List list = new ArrayList();

This sentence creates an object of the ArrayList implementation class and traces it up to the List interface. At this time, it is a List object. It has some properties and methods that the ArrayList class has, but the List interface does not have. It can no longer be used, and

ArrayList list=newArrayList();
Copy after login

creating an object retains all the properties of the ArrayList. and methods.

Test.java
public class Test {

    public static void main(String[] args) throws ClassNotFoundException {
        Animals a2 = new Dog();
        a2.shout();
        a2.guard();
    }
}
Copy after login

The difference between list and arraylist in java

The parent class can no longer call methods that appear in the parent class but not in the subclass. Otherwise, compilation errors will occur.

If it looks like this:

List a=new ArrayList();
Copy after login

Then a has all the properties and methods of List, and will not have the unique properties and methods of its implementation class ArrayList.

If List and ArrayList have the same attributes (such as int i) and the same method (such as void f()),

then a.i calls i and a.f in List () is calling f() in ArrayList;

The key to the problem:

Why should we use List list = new ArrayList() Instead of ArrayList alist = new ArrayList()?

The problem is that the List interface has multiple implementation classes. Now you are using ArrayList. Maybe one day you need to switch to other implementation classes, such as:

LinkedList or Vector, etc. At this time, you only need to change this line: List list = new LinkedList(); Other codes where list is used do not need to be changed at all.

Suppose you start using ArrayList alist = new ArrayList(), now you have made some changes, especially if you use the methods and properties unique to the ArrayList implementation class.

Recommended tutorial: Java tutorial

The above is the detailed content of The difference between list and arraylist in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template