Catalogue of Series Articles
No, there may be follow-up later.
Preface
Recently, looking at the source code of the Java container, I saw the Arrays class. Combining with the differences of List, Array, Arrays, ArrayList which I have learned a while ago, Baidu made a comparison between them. They are both two comparisons. Let's record them here.
PS: Because I also write while learning, the article may not be very logical, that is, where I remember it
1. List interface
List is an interface
His function declaration in jdk8 is like this
public interface List<E> extends Collection<E>
Simple inheritance, it feels like the java containers, except for those under map
He cannot instantiate an object, but we can create an object reference for the List that points to him and instantiates it with its subclasses
//Correct usage List list = new ArrayList(); List list1 = new LinkedList(); //Wrong usage List list = new List();
2. Array class
This statement is cleaner than List
public final class Array
Nothing here but look up at the notes
The Array class provides static methods to dynamically create and access Java arrays.
It can be seen here that the Array class only provides static methods to dynamically create and access Java arrays. Its structure is also static. Check it on the Internet, and many people think of it as one of the most basic storage structures in Java, so let's start with that.
The constructor permission here is private, indicating that this class cannot be instantiated and can only be considered a toolbox
/** * Constructor. Class Array is not instantiable. */ private Array() {}
Another thing to note is that since Array is essentially an array, its capacity is fixed and cannot be dynamically changed
3. Static Class Arrays
Function declarations are still okay
public class Arrays
If you can't get information here, look up the note
This class contains various methods for manipulating arrays (such as sorting and searching). This class also contains a static factory that allows arrays to be viewed as lists.
This class contains various methods for manipulating arrays, such as sorting and searching. This class also contains a static function that allows arrays to be viewed as lists.
Simply put, it's a tool class for arrays
In other words, this function of array to list seems to have been seen before, and it turned down and found
/** * Returns a fixed-size list backed by the specified array. (Changes to * the returned list "write through" to the array.) This method acts * as bridge between array-based and collection-based APIs, in * combination with {@link Collection#toArray}. The returned list is * serializable and implements {@link RandomAccess}. * * <p>This method also provides a convenient way to create a fixed-size * list initialized to contain several elements: * <pre> * List<String> stooges = Arrays.asList("Larry", "Moe", "Curly"); * </pre> * * @param <T> the class of the objects in the array * @param a the array by which the list will be backed * @return a list view of the specified array */ @SafeVarargs @SuppressWarnings("varargs") public static <T> List<T> asList(T... a) { return new ArrayList<>(a); }
Well, that's it, right. ||
One thing to note here is Arrays.asList does return an ArrayList object, but it is a private static internal class (java.util.Arrays.ArrayList) in the Arrays class, not the common java.util.ArrayList class, this internal class does not have any methods to add or remove elements
Notice, however, that the constructor of the ArrayList class can accept the type of Collection class and its subclasses
public ArrayList(Collection<? extends E> c) {
So if we need to convert Array to ArrayList we can do this
String[] str = new String[] {"a", "b", "x"}; List<String> list = new ArrayList<>(Arrays.asList(str));
4. ArrayList Classes
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
ArrayList is a dynamic array, which is a complex version of the array. It dynamically adds and deletes elements. You can see from the function declaration that ArrayList implements java.util.Collections.Collection.List interface
At the bottom of the ArrayList is an array of Object types
/** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. Any * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA * will be expanded to DEFAULT_CAPACITY when the first element is added. */ transient Object[] elementData; // non-private to simplify nested class access
Look again at the declaration of the insert function
public boolean add(E e)
You can see that this ArrayList can be added to different types of elements without using generics, and it can be without specifying a length.
ArrayList is a concrete implementation of List, so there is no need to compare it to List, and there are several points to compare it to ArrayList:
- length
Array's length is immutable
The length of the ArrayList can be changed (automatically doubles if there is not enough space) - Stored data type
ArrayList can store heterogeneous objects, while Array can only store data of the same data type. - efficiency
Adding and modifying elements to an ArrayList can result in boxing and unboxing operations. Frequent operations may affect some of the efficiency, which is less efficient than a fixed length of Array.
Let's start with that. It's too late. Write a summary tomorrow.
Blogging takes a bit of time==, I read an array one night