ArrayList collection concept

The ArrayList class is an array that can be modified dynamically. Unlike normal arrays, there is no fixed size limit. You can add or remove elements.

ArrayList inherits AbstractList and implements the List interface.

The ArrayList class is in the java.util package. You need to import the java.util package before using the ArrayList class.Copy the code

ArrayList initialization

ArrayList<E> objectName =new ArrayList<>();Copy the code

Where E stands for generic, representing the data types (reference types only) stored in the ArrayList collection. Starting with JDK1.7, the Angle brackets on the right side can no longer write content.

The method of ArrayList

An ArrayList is an array queue that provides adding, deleting, modifying, traversing, and so on. Here are just a few common methods, others can be used by referring to the API documentation.

Let’s start by creating a collection of ArrayLists for the following explanation.

ArrayList<String> arrayList  = new ArrayList<String>();
Copy the code

Add elements

Adding elements to an ArrayList can be done using the add() method

AddAll () adds all the elements in the collection to the ArrayList at the specified locationCopy the code
Arraylist.add (" dilieba "); // Add the element arrayList.add(" Dilieba "); // Add the element arrayList1.add(" Yang min ") to arrayList1; Arraylist. addAll(arrayList1); // addAll the elements of arrayList1 to arrayList.Copy the code

Access to the elements

Elements in an ArrayList can be accessed using the get() method

Get () gets the elements in the ArrayList by index valueCopy the code
for (int i = 0; i < arrayList.size(); i++) {
   System.out.println(arrayList.get(i));
}
Copy the code

Modify the element

If you want to modify elements in an ArrayList, you can use the set() method

Set () replaces the element of the specified index in the ArrayListCopy the code
Arraylist.set (0," arrayList.set ");Copy the code

Remove elements

If you want to remove an element from an ArrayList, use the remove() method

Remove () removes a single element from the collection removeAll() removes all elements that exist in the arrayList collection from the specified collection clear() removes all elements from the arrayListCopy the code
Arraylist.remove (0); // Delete the first element of the arrayList collection. Arraylist. removeAll(arrayList1); // Delete the element arrayList. Arraylist.clear (); // Delete all elements from arrayList;Copy the code

ArrayList sorting

The Collections class is also a very useful class, housed in the java.util package, that provides a sort() method to sort lists of characters or numbers.

Sort () sorts the ArrayList collectionCopy the code
//使用Collection类的sort方法给集合排序
Collections.sort(arrayList);
//使用sort方法进行升序排序
arrayList.sort(Comparator.naturalOrder());
//使用sort方法进行降序排序
arrayList.sort(Comparator.reverseOrder());
Copy the code

That’s all I have to say about ArrayList collections. If there are any errors, please comment.