[Java common classes] common methods of ArrayList and Vector in java.util package

Article directory




Preface

ArrayList and Vector are used the same way. They are all variable size arrays.

Difference:

  • Thread safety: ArrayList is thread unsafe, Vector thread safe
  • Speed: ArrayList is more efficient. Vector is less efficient than ArrayList because it is thread safe.

If you need a thread safe ArrayList, don't use Vector (not as good as the latter). Please use the CopyOnWriteArrayList class under the JUC package.

1, ArrayList

The implementation of variable size array of List interface. Implements all optional List operations and allows all elements including null. In addition to implementing the List interface, this class also provides methods to manipulate the size of the array used internally to store the List. (this class is roughly equivalent to the Vector class, except that it is not synchronized.)

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, Serializable

Two, use

1. constructor

Create an ArrayList with a default capacity of 10 and an ArrayList with an initial capacity of 20

        // Create a default ArrayList
        ArrayList<Object> objects = new ArrayList<>();

        // Create an ArrayList with an initialization capacity of 20
        ArrayList<Object> objects1 = new ArrayList<>(20);

2. Common methods

2.1 add + get + remove

add(E e)
Adds the specified element to the end of this list.
add(int index, E element)
Inserts the specified element into the specified location in this list.
get(int index)
Returns the element at the specified location in this list.
remove(int index)
Removes the element at the specified location in this list.
remove(Object o)
Removes the specified element, if it exists, that first appears in this list.

add(), get(), remove()

        ArrayList<Integer> arr = new ArrayList<>();
        // Add to
        arr.add(0);
        arr.add(1);
        arr.add(2);
        System.out.println(arr); // [0, 1, 2]
        // Add to specified location
        arr.add(1,111);
        System.out.println(arr); // [0, 111, 1, 2]

        // Obtain
        Integer i0 = arr.get(0);
        System.out.println(i0); // 0

        // Remove (by element's subscript)
        arr.remove(0); 
        System.out.println(arr); // [111, 1, 2]
        // Remove by element
        arr.remove((Integer)111); // 
        System.out.println(arr); // [1, 2]
2.2 get the number of elements

size()
Returns the number of elements in this list.

        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(0);
        arr.add(1);
        arr.add(2);
        // Get the number of elements
        int size = arr.size();
        System.out.println(size); // 3
2.3 whether it is empty

isEmpty()
Returns true if there are no elements in this list

        ArrayList<Integer> arr = new ArrayList<>();
        boolean empty = arr.isEmpty();
        System.out.println(empty); // true
2.4 included or not

contains(Object o)
Returns true if the list contains the specified element.

        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(12345);
        boolean contains = arr.contains(12345);
        System.out.println(contains);
2.5 search

indexOf(Object o)
Returns the index of the specified element that first appears in this list, or - 1 if the list does not contain an element.
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list, or - 1 if the list does not contain an index.

Two searches, return subscript found, return - 1 not found

        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(1);
        // Find where the element first appears
        int i = arr.indexOf((Integer) 1);
        System.out.println(i); // 0
        // Find the last occurrence of an element
        int i1 = arr.lastIndexOf((Integer) 1);
        System.out.println(i1); // 3
2.5 replacement

set(int index, E element)
Replaces the element at the specified location in this list with the specified element.

        ArrayList<Integer> arr = new ArrayList<>();
        arr.add(1);
        // Replace the element value of the specified subscript location
        arr.set(0, 12345);
        System.out.println(arr);

Last

The methods of these data structures are almost the same. Many of them can be used in a simple way.


Relevant

For more common classes, see: [Java common classes] directory

123 original articles published, 144 praised, 20000 visitors+
Private letter follow

Keywords: less Java

Added by byronwells on Sat, 01 Feb 2020 14:46:35 +0200