Home Java JavaBase The difference between list and arraylist in java

The difference between list and arraylist in java

Nov 18, 2019 pm 01:17 PM
arraylist java list the 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();
    }
}
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!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How to set password protection for export PDF on PS How to set password protection for export PDF on PS Apr 06, 2025 pm 04:45 PM

Export password-protected PDF in Photoshop: Open the image file. Click "File"> "Export"> "Export as PDF". Set the "Security" option and enter the same password twice. Click "Export" to generate a PDF file.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Why do you need to call Vue.use(VueRouter) in the index.js file under the router folder? Apr 05, 2025 pm 01:03 PM

The necessity of registering VueRouter in the index.js file under the router folder When developing Vue applications, you often encounter problems with routing configuration. Special...

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles