search
HomeJavajavaTutorialHow to define and implement ArrayList and sequence list in Java

    1. Linear table

    Definition

    Linear table is the most basic, simplest, and most commonly used data structure. A finite sequence, which contains n data elements with the same characteristics, is called a linear list and is a type of data structure.

    Common linear lists: sequential lists, linked lists, stacks, queues...

    Linear lists are logically linear structures, that is to say, they are a continuous straight line. The physical storage form of linear tables is usually an array or linked list structure, but it is not necessarily continuous.

    Features

    • #There must be only one "first element" in the set.

    • There must be only one "last element" in the set.

    • Except for the last element, all elements have a unique successor (successor).

    • Except for the first element, all elements have a unique predecessor (antecedent).

    2. Sequence table

    Definition

    The linear structure usually stored in the form of an array is called a sequence table, which stores data elements in sequence in a physical in memory cells with consecutive addresses. Complete the addition, deletion, checking and modification of data on the array.

    Implementation

    First we need to create an array to store data.

    How to define and implement ArrayList and sequence list in Java

    Note: Because I created the integer array first for convenience, in order to better adapt to various types, you can create a generic array, which I don’t have here. wrote.

    How to define and implement ArrayList and sequence list in Java

    The next step is to perform various operations on the sequence table. For example: basic CURD, print sequence table, get sequence table length, clear sequence table, etc.

    Because it is an array, just traverse the array and print it directly

    How to define and implement ArrayList and sequence list in Java

    Add new elements

    When adding elements, it is necessary to consider whether the array is full, so we need to make a judgment. If the array space is full, it needs to be expanded. In addition, we also need to determine whether this pos position is legal.

    Method to determine whether the space is full

    How to define and implement ArrayList and sequence list in Java

    Here we simplify the code as:

    How to define and implement ArrayList and sequence list in Java

    If you want In the case of expansion, after the expansion is completed, because the sequence list is a continuous structure, if a new element is added at the pos position, the elements after the pos position will be moved back in sequence. Only in this way can new elements be added.

    How to define and implement ArrayList and sequence list in Java

    Note: After expansion, we need to change the size of CAPACITY and usedSize.

    Determine whether a certain element is contained

    Here we need to consider whether the array is empty at this time.

    How to define and implement ArrayList and sequence list in Java

    After that, it is still a direct traversal of the array.

    How to define and implement ArrayList and sequence list in Java

    Finding elements

    A null operation is also required here.

    How to define and implement ArrayList and sequence list in Java

    Get the element at pos position

    There may be situations where the array is empty and pos is illegal, so judgment is required.

    I am manually throwing exceptions here, and I have not written anything else.

    How to define and implement ArrayList and sequence list in Java

    Change the value of pos position

    How to define and implement ArrayList and sequence list in Java

    Delete operation

    Delete an element at a certain position , you can directly let the elements behind it cover it to achieve deletion.

    How to define and implement ArrayList and sequence list in Java

    Get the sequence table length

    How to define and implement ArrayList and sequence list in Java

    Clear the sequence table

    How to define and implement ArrayList and sequence list in Java

    The following operations are relatively simple and will not be described in detail.

    3. ArrayList

    Introduction:

    In the collection framework, ArrayList is an ordinary class that implements the List interface. The specific framework diagram is as follows:

    How to define and implement ArrayList and sequence list in Java

    [Explanation]

    1. ArrayList implements the RandomAccess interface, indicating that ArrayList supports random access.

    2. ArrayList implements the Cloneable interface, indicating that ArrayList can be cloned.

    3. ArrayList implements the Serializable interface, indicating that ArrayList supports serialization.

    4. Unlike Vector, ArrayList is not thread-safe and can be used in a single thread. In multi-threads, you can choose Vector or CopyOnWriteArrayList.

    5. The bottom layer of ArrayList is a continuous space and can be dynamically expanded. It is a dynamic type sequence list.

    Using

     public static void main(String[] args) {
            // ArrayList创建,推荐写法
            // 构造一个空的列表
            List<Integer> list1 = new ArrayList<>();
     
            // 构造一个具有10个容量的列表
            List<Integer> list2 = new ArrayList<>(10);
            list2.add(1);
            list2.add(2);
            list2.add(3);
     
            // list2.add("hello"); // 编译失败,List<Integer>已经限定了,list2中只能存储整形元素
            // list3构造好之后,与list中的元素一致
            ArrayList<Integer> list3 = new ArrayList<>(list2);
     
            // 避免省略类型,否则:任意类型的元素都可以存放,使用时将是一场灾难
            List list4 = new ArrayList();
            list4.add("111");
            list4.add(100);
        }

    Some common methods

    Method Explanation
    boolean add(E e) End insert e
    void add(int index, E element) will e is inserted into the index position
    boolean addAll(Collection extends E> c) Insert the end of the elements in collection c into the collection
    E remove(int index) Remove the index position element and return
    boolean remove(Object o) Delete the first o
    E get(int index) Get the subscript index position element
    E set(int index, E element) Set the subscript index position element to element
    void clear() Clear the sequence table
    boolean contains(Object o) Judge whether o is in the linear table
    int indexOf(Object o) Return the subscript of the first o
    int lastIndexOf(Object o) Return the subscript of the last o
    List subList(int fromIndex, int toIndex) Interception of part of the list
    ## ArrayList traversal

    Loop traversal

    How to define and implement ArrayList and sequence list in Java

    foreach traversal

    How to define and implement ArrayList and sequence list in Java##Iterator

            System.out.println("======迭代器1=========");
     
            ElementObservableListDecorator<Object> list;
            Iterator<String> it =  list.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }
            System.out.println("======迭代器2=========");
            ListIterator<String> it2 =  list.listIterator();
            while (it2.hasNext()) {
                System.out.println(it2.next());
            }

    Sequence table and The difference between arrays:

    As mentioned above, the bottom layer of the sequence table can be understood as an array, but it is more advanced than an array.

    The sequence table can expand by itself;

    The sequence table strictly distinguishes between the array capacity and the number of elements.

    So an array is actually an incomplete sequence list.

    Notes in the sequence table:

      We need to distinguish between two concepts in the sequence table: capacity and the number of elements (size).
    • Capacity can be understood as the size (length) of the array, and the number of elements is the number of valid elements recorded in size.
    • In a sequence table, data storage needs to be continuous, and there cannot be "gaps" between elements. When operations such as insertion and deletion are performed, after the operation is completed, Ensure the continuity of the sequence list.

    The above is the detailed content of How to define and implement ArrayList and sequence list in Java. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
    带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

    Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

    完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

    一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

    详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

    本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

    Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

    java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

    封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

    归纳整理JAVA装饰器模式(实例详解)归纳整理JAVA装饰器模式(实例详解)May 05, 2022 pm 06:48 PM

    本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。

    See all articles

    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

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Repo: How To Revive Teammates
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    Hello Kitty Island Adventure: How To Get Giant Seeds
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    Powerful PHP integrated development environment

    SAP NetWeaver Server Adapter for Eclipse

    SAP NetWeaver Server Adapter for Eclipse

    Integrate Eclipse with SAP NetWeaver application server.

    EditPlus Chinese cracked version

    EditPlus Chinese cracked version

    Small size, syntax highlighting, does not support code prompt function

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor