JAVA: Basic knowledge of List and common functions

Table of contents

1: Basic knowledge

2: Properties of List 

Three:ArrayList

3.1Basic knowledge of ArrayList and how to create an ArrayList

3.2 Properties of ArrayList

3.3Dynamic expansion mechanism of ArrayList

3.4 Common methods of ArrayList:


1: Basic knowledge

       In Java, List is an interface that represents an ordered collection and allows the storage of duplicate elements. The List interface inherits from the Collection interface, so it has all the methods of the Collection interface, and also provides some additional methods to handle elements in an ordered set.

java.util.CollectionThe interface is one of the root interfaces of the Java collection framework. It defines a set of common methods for operating elements in the collection. The Collection interface is the common parent interface for all collection classes in the Java collections framework.

2: Properties of List 

2.1. Orderliness: The elements in List are maintained in the order in which they are inserted, Elements can be accessed by index. This means that you canget, insert, delete, or replace elementsin a specific order.

2.2 Repeatability: Unlike Set, List allows to store repeated elementsList< a i=4>. This means that the same element can be added multiple times within .

2.3 Mutability: List is mutable and elements can be added, deleted and modified dynamically The size and content.

Three:ArrayList

3.1ArrayList basics and how to create an ArrayList

   ArrayList is a commonly used implementation class in the Java collection framework. Its underlying logic is implemented through arrays. It implements List interface provides the function of dynamic array. When we create a List object, we often write the following paradigm:

List list = new ArrayList();

        No one has ever seen List list = new List(); written this way. Why is this?

        In Java , List is an interface (java.util.List) , rather than a concrete class. The interface itself cannot be directly instantiated as an object because it is just a declaration of a set of methods without actual method implementation. So if we want to implement List, we need to make an upward cast, where is a subclass of the interface. Assigning an object of class to a reference to the interface is an upward casting operation. This allows programmersto use the methods of the interface to operate objects,< /span> interface. interface, it is an instance of the implements the Because ArrayListListArrayListListListArrayListArrayListListList

3.2 Properties of ArrayList

3.2.1 Dynamic array: ArrayList is implemented based on dynamic array, which allows storage and operation of a variable number of elements. The capacity of the array automatically grows or decreases to accommodate the addition and removal of elements, so there is no need to manually manage the size of the array.

3.2.2 Ordering: ArrayList is an ordered set, which maintains the order of elements in the order in which they are inserted. This means that elements can be accessed and traversed in the order they were added.

3.2.3 Allow duplicate elements: ArrayList Allows storage of duplicate elements, that is, the same element can appear multiple times in the list.

3.2.4 Random access: Since ArrayList is based on array implementation, it supports random access. Elements in the list can be accessed directly through indexing.

3.2.5 Thread unsafe: ArrayList is not thread-safe, which means that in a multi-threaded environment, if multiple threads access at the same time and modifying the same ArrayList instance may result in data inconsistency or anomalies. If you need to use it in a multi-threaded environment, you can consider using the Collections.synchronizedList() method to get a thread-safe ArrayList.

3.2.6 Performance: Since ArrayList supports random access, it is suitable for scenarios that require frequent access to elements. Very efficient. However, performance may be poor in situations where frequent insertion or deletion of elements is required, as this involves moving array elements.

3.2.7Dynamic expansion: WhenArrayList the capacity of the internal array is not enough to accommodate new elements, It automatically expands the size of the array, usually half its current capacity. This avoids frequent array reallocation operations and improves performance.

3.3Dynamic expansion mechanism of ArrayList

   ArrayList's expansion mechanism is a key part of its internal implementation of dynamic arrays, which allowsArrayList to automatically increase the capacity of the underlying array when needed to accommodate The need to add elements avoids frequent array reallocation operations and improves performance. The following is a detailed introduction to the expansion mechanism of ArrayList:

  1. Initial capacity: ArrayList At creation time, an initial capacity (e.g. 10 elements) is usually allocated, even if the list is empty. This is to avoid an immediate expansion operation when there is only one element in the list.

  2. Add element: When you add an element to ArrayList, it will first check whether the current number of elements reaches the capacity of the underlying array. If the current number of elements reaches the capacity of the underlying array, it needs to be expanded.

  3. Expansion strategy: ArrayList Use the following expansion strategy:

            When expansion is needed, it will create a new array with a larger capacity, and the size of the new array is usually 1.5 times the current capacity (can be adjusted by increment factor, default is 1.5). Copies all elements in the current array to the new array. The new array replaces the old array as the underlying array of ArrayList.
  4. Performance Analysis: The expansion operation is a relatively expensive operation in ArrayList because it requires copying all elements from the old array to the new array. Typically, the cost of expansion is O(n), where n is the current size of ArrayList.

3.4 Common methods of ArrayList:

3.4.1 add(E element): Add an element to the end of the list.

 

 3.4.2: add(int index, E element): Insert an element at the specified position. The subscript of the list starts from 0. If the index exceeds the index range, an exception will be reported.

3.4.3 get(int index): Returns the element at the specified index position.

3.4.4 remove(int index): Remove the element at the specified index position.

3.4.5 size(): Returns the number of elements in the list.

3.4.6 isEmpty(): Check whether the list is empty.

3.4.7      contains(Object element): Check whether the list contains the specified element.

3.4.8  indexOf(Object element): Returns the index of the first occurrence of the specified element, or -1 if it does not exist.

3.4.9  lastIndexOf(Object element): Returns the index of the last occurrence of the specified element, or -1 if it does not exist.

Guess you like

Origin blog.csdn.net/jialuosi/article/details/133272402