ARRAYList in Jin Collections detailed explanation and usage method
ARRAYList in Jin Collections detailed explanation and usage method
In the Java language, ArrayList is a commonly used data structure that belongs to a class in the Jin Collections framework.ArrayList is a dynamic array that can automatically adjust the size as required and support random access to elements.This article will introduce the characteristics, usage methods, and some example code of ArrayList.
Features of ArrayList:
1. Dynamic size: ArrayList can automatically adjust the size as needed without manual operation of the array length.This makes ArrayList very convenient, especially when the element that needs to be processed.
2. Random access: Similar to the array, ArrayList allows access elements by indexing, that is, supporting random access.This means that it can quickly access and operate the elements in the index value of the element.
3. Automatic expansion: When the capacity of ArrayList is not enough to accommodate new elements, it automatically expands the size of the internal array.By default, the current capacity will be increased by 50%when expanding capacity.In this way, ArrayList can flexibly adapt to changing demand and avoid the problem of insufficient array.
4. Allow duplicate elements: Unlike Set, ArrayList allows storage of repeated elements.This makes ArrayList very useful in the scenario that needs to be preserved in multiple elements.
How to use ArrayList:
1. Create ArrayList objects: You can use a non -constructor creation function to create an empty ArrayList object, or you can specify the initial capacity at the time of creation.For example:
ArrayList<String> list = new ArrayList<>();
2. Add element: You can use the ADD () method to add elements to ArrayList.When adding elements, ArrayList will automatically adjust the size.For example:
list.add ("Element 1");
list.add ("Element 2");
list.add ("Element 3");
3. Obtain element: You can use the Get () method to obtain elements of a specific position according to the index.For example:
String element = list.get(0);
4. Modify element: You can use the set () method to modify the elements in ArrayList according to the index.For example:
list.set (0, "new element");
5. Delete element: You can use the Remove () method to delete the elements in ArrayList according to the index or elemental value.For example:
list.remove(0);
list.remove ("Element 2");
6. Determine whether the element exists: you can use the contains () method to determine whether ArrayList contains the specified element.For example:
boolean isExist = list.contains("元素1");
7. Get the list size: You can use the size () method to obtain the number of elements in ArrayList.For example:
int size = list.size();
8. Traversing ArrayList: You can use the Iterator, For-Each cycle, or ordinary for cycle to traverse elements in ArrayList.For example:
// Use Iterator to traverse
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
}
// Use For-Each to cycle traversal
for (String element : list) {
}
// Use ordinary for recycling traversal
for (int i = 0; i < list.size(); i++) {
String element = list.get(i);
}
In summary, ArrayList is a commonly used data structure in Java. It has the characteristics of dynamic size, random access, automatic capacity expansion, and allowed repetitive elements.By mastering its usage, we can facilitate the facilitation of elements that are not sure of the number of elements, and provide convenience for development.
I hope this article can help you understand ArrayList and use it flexibly in actual development.