The problem:

Arrays are stored consecutively in memory. Because of this, it’s easy to find a value in an array just by knowing the array subscript (note: array subscripts start at 0), and it’s also quick to change a value in an array. But it also has its drawbacks: deleting and inserting elements is extremely complex, requiring moving the original elements and taking time. You must first know the size of an array, and when you declare an array, you must also specify its length, as is the case with dynamically allocating memory.

Solutions:

The ArrayList class implements a growable dynamic array located in java.util.arrayList. Implements the List interface, which can store objects of different types (including NULL), while arrays can only store values of specific data types.

import java.util.ArrayList
ArrayList list = new ArrayList();
list.add(111);
list.add("abc");
list.remove(0);
Copy the code
  1. The List interface is a subinterface of the Collection interface. The List has an important implementation class – ArrayList class. The elements in the List are ordered and repeatable, so they are called sequences.

  2. List can precisely control the insertion position of each element, or delete a location element, its implementation class ArrayList is implemented by arrays.

  3. There are ways to add, delete, modify and check in the List.

  4. List is an interface and cannot be instantiated. You need a concrete class to instantiate it.

  5. The objects in the List are arranged in a certain order, and the contents can be repeated. The List interface implements the following classes: ArrayList (implements dynamic arrays), Vector (implements dynamic arrays), LinkedList (implements linked lists), and Stack (implements stacks).

  6. Lists can also store objects of different types.

    List list; // List = null;

    List list = new List(); // Is the wrong way to use it

    List list = new ArrayList(); ArrayList is now a List object. ArrayList has properties and methods that ArrayList doesn’t have, so you can’t use them. List List =new ArrayList()

Benefits of writing like this:

  1. You can always change to another implementation class as needed, such as LinkedList or Stack, etc., just change this line: List List = new LinkedList(); The rest of the code that uses the list doesn’t need to be changed.

  2. So it’s highly recommended that you don’t write ArrayList list = new ArrayList(), which would be very cumbersome to change (if you implement arrayList-specific methods and properties, you’d have to change a lot of things), very cumbersome. This is also an important object oriented knowledge point: polymorphism. Interfaces and abstract classes cannot be instantiated, but they can create a reference to their own object. The interface and abstract classes implement classes or subclasses that do this, and of course they can instantiate objects by implementing classes and subclasses. That’s what polymorphism is. The above example also shows the advantages of polymorphism

There’s another way to declare a List.

List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
Copy the code

However, this declaration has some drawbacks. It uses asList to convert an array to a List. Defect: When you use the arrays.asList () utility class to convert Arrays to lists, you can’t use it to modify list-related methods. It’s the add/remove/clear method throws UnsupportOperationException abnormalities This is because asList return objects is a Arrays inner class, did not modify the List of methods. Arrays.aslist is an adaptor pattern, just a conversion interface, and the data behind it is still an array.