This article explains how to use the ArrayList class in Java with an example.
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.
1.ArrayList Constructors
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.
2.ArrayList Common methods
The following is a summary of some of the more commonly used ArrayList class member methods:
- 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).
These are the more member methods used in the ArrayList class. For a more detailed description of each method or other methods not mentioned, readers can refer to the Java official API
3.ArrayList usage Examples
1 ImportJava.util.*;2 3 Public classArraylistexamples {4 5 Public Static voidMain (String args[]) {6 //creates an empty array-linked list object list,list to hold data of type string7arraylist<string> list =NewArraylist<string>();8 9 //adding elements to the list objectTenList.add ("Item1"); OneList.add ("Item2"); AList.add (2, "Item3");//This statement will add the "Item3" string to the 3rd position of the list. -List.add ("Item4"); the - //Display the contents of an array of linked lists -System.out.println ("The ArrayList contains the following elements:" -+list); + - //Check the position of the element + intpos = List.indexof ("Item2"); ASYSTEM.OUT.PRINTLN ("The Index of ITEM2 is:" +POS); at - //checks if the array list is empty - BooleanCheck =List.isEmpty (); -System.out.println ("Checking If the ArrayList is empty:" +check); - - //get the size of the list in intSize =list.size (); -System.out.println ("The size of the list is:" +size); to + //checks whether an array contains an element in the list of lists - Booleanelement = List.contains ("ITEM5"); the System.out *. println ("Checking If the ArrayList contains the object ITEM5:" $+element);Panax Notoginseng - //gets the element at the specified position theString item = list.get (0); +SYSTEM.OUT.PRINTLN ("The item is the index 0 is:" +item); A the //traversing elements in a ArrayList + - //1th method: Iterating over the index of the element and the size of the linked list $ System.out $. println ("Retrieving items with loop using index and Size list"); - for(inti = 0; I < list.size (); i++) { -System.out.println ("Index:" + i + "-Item:" +List.get (i)); the } - Wuyi //2nd method: Using the Foreach Loop theSystem.out.println ("Retrieving Items using foreach Loop"); - for(String str:list) { WuSystem.out.println ("Item is:" +str); - } About $ //Third method 3rd way:using iterator - //Hasnext (): Fanhuitrue indicates that there are elements in the list of links - //Next (): Returns the next element -System.out.println ("Retrieving Items using iterator"); A for(Iterator<string> it =list.iterator (); It.hasnext ();) { +System.out.println ("Item is:" +It.next ()); the } - $ //Replace Element theList.set (1, "NewItem"); theSystem.out.println ("The ArrayList after the replacement is:" +list); the the //remove Element - //removing the item in index 0 inList.remove (0); the the //removing the first occurrence of item "ITEM3" AboutList.remove ("Item3"); the theSystem.out.println ("The final contents of the ArrayList is:" +list); the + //converting ArrayList to Array -string[] Simplearray = List.toarray (Newstring[list.size ()]); theSystem.out.println ("The array created after the conversion of our ArrayList is:"Bayi+arrays.tostring (Simplearray)); the } the}
Getting Started with Java: examples explaining ArrayList usage