search
HomeJavaJavaBaseThe difference between list and arraylist in java

The difference between list and arraylist in java

Nov 18, 2019 pm 01:17 PM
arraylistjavalistthe difference

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();
    }
}

The difference between list and arraylist in java

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

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();

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();
    }
}

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();

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools