ArrayList
One
The Java.util.ArrayList class is a dynamic array type, meaning that the ArrayList object has both an array and a list of features. You can add or remove an element from the linked list at any time. The ArrayList implements the list interface.
As you know, the array is static, and after the array is initialized, the length of the array can no longer be changed. ArrayList can be changed dynamically in size. So, when to use Array (array), when to use ArrayList? The answer is: when we don't know how many data elements there are, we can use ArrayList, and if we know how many elements the data set has, we use arrays.
Two
The ArrayList class supports 3 construction methods.
This construction method constructs an empty linked list.
ArrayList(Collection<? extends E> c)
This constructor constructs a linked list containing the specified set of elements, noting that the character e here is a token that represents the type of the element in the collection. As to what the specific type is, you need to specify it when you use this construction method.
· ArrayList(int initialCapacity)
This is the third construction method, which constructs a list with a specified size but empty contents. The initialcapacity parameter is the initial capacity size.
For example, if you are creating an empty list of arrays to hold objects of type string, you can do this as follows:
arraylist<string> list = new arraylist<string> ();
If you need to create an array-linked list that specifies the initial capacity, you can do this as follows:
arraylist<integer> list = new arraylist<integer> (7);
Note: The ArrayList class supports only object types and does not support underlying data types. This means that the ArrayList object can only hold objects and cannot hold data of the underlying data type.
Three
- Adding elements to a linked list
boolean add(Element e)
Adds the specified element to the tail of the linked list.
void add(int index, Element e)
Adds the specified element to the linked list at the specified location.
- Remove an element from a linked list
void clear()
Removes all elements from the linked list.
E remove(int index)
Deletes the element at the specified position in the linked list.
protected void removeRange(int start, int end)
Deletes an element in a linked list that ends at a location from one location to another.
- Get the elements in a linked list
E get(int index)
Gets the element at the specified position in the linked list.
Object[] toArray()
Gets an array in which all elements in the array are elements of the linked list. (Converting a linked list to an array)
- Modify an Element
E set(int index, E element)
Replaces the element at the specified position in the list with a new element.
- Searching for elements
boolean contains(Object o)
Returns true if the linked list contains the specified element.
int indexOf(Object o)
Returns the position of the first occurrence of the element in the list, if 1 is returned, indicating that the element is not in the linked list.
int lastIndexOf(Object o)
Returns the position of the last occurrence of the element in the list, if 1 is returned, indicating that the element is not in the linked list.
- Check if the linked list is empty
boolean isEmpty()
Returns true to indicate that there are no elements in the linked list.
- Get linked list size
int size()
Returns the length of the linked list (the number of elements in the list).
Java---ArrayList