Class ArrayList. Dynamic array. General information. Creating an array
Contents
- 1. List of methods of the ArrayList class
- 2. Class constructors. Creating an array. Example
- Related topics
Search other resources:
1. List of methods of the ArrayList class
The ArrayList class is a dynamic array. In a dynamic array, elements are added and removed as needed, unlike standard arrays. In a standard array, you cannot change the number of array elements. To do this, you need to create a new array with a new size in another memory area and copy the data of the original standard array into it. Dynamic arrays are effective in cases where the size of the array (data) is unknown at the beginning of the program execution. This size is formed as needed.
The ArrayList class implements the List interface and has the following declarations:
class ArrayList<E>
where E is the type of objects to be stored.
The list of common class methods is as follows:
- add – add an element to the array;
- addAll – add a set to the array;
- clear – clear the array;
- clone – get a copy of the array;
- contains – determine if the list contains a certain element;
- containsAll – determine whether all elements of some collection are in the given collection;
- ensureCapacity – reserve a memory fragment for the array;
- get – get array element;
- indexOf – determine the position of the first occurrence of the element in the array;
- isEmpty – determine if the array is empty;
- iterator – get an iterator to the array;
- lastIndexOf – determine the position of the last occurrence of the element in the array;
- listIterator – get the iterator as a list;
- remove – remove the element at the given position;
- removeAll – remove a group of elements from the collection;
- removeIf – change the collection based on the predicate;
- replaceAll – perform calculation over each element of the array;
- retainAll – form a new array containing the elements of the specified collection;
- set – set a new value in the array;
- size – get the size of the array;
- sort – sort array elements in the given order;
- subList – get an array fragment based on the given array;
- toArray – convert an array to an array of type Object[];
- trimToSize – correct the current size of the array.
⇑
2. Class constructors. Creating an array. Example
The following constructors are defined in the ArrayList class:
ArrayList() ArrayList(Collection<? extends E>) ArrayList(int size)
here
- E – type of collection elements;
- size – the current size of the array.
The first constructor creates an empty dynamic array. The second constructor creates a dynamic array based on another array.
The third constructor creates an empty array with a reserved amount of size size. If, when growing, the number of elements in such an array exceeds size, then the reserved volume (maximum capacity) will be increased by some amount.
Example. The example creates different types of dynamic arrays.
import java.util.*; public class TrainCollections { public static void main(String[] args) { // 1. Constructor ArrayList() // Create an empty array of integers ArrayList<Integer> AL = new ArrayList(); // Add numbers from 0 to 9 to an array for (int i=0; i<10; i++) AL.add(i); System.out.println(AL); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] // 2. Create an array of strings based on another array of strings, // constructor ArrayList(Collection<? extends E>) // 2.1. Create a source array ArrayList<String> AS1 = new ArrayList(); AS1.add("Winter"); AS1.add("Spring"); AS1.add("Autumn"); AS1.add("Summer"); System.out.println(AS1); // [Winter, Spring, Autumn, Summer] // 2.2. Use constructor ArrayList(Collection<? extends E>) ArrayList<String> AS2 = new ArrayList(AS1); System.out.println(AS2); // [Winter, Spring, Autumn, Summer] // 3. Constructor ArrayList(int) // 3.1. Create an empty array with a reserved size of 8 elements ArrayList<Character> AC = new ArrayList(8); // 3.2. Get the size of an array System.out.println(AC.size()); // 0 - this is the current size of the array } }
Program result
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [Winter, Spring, Autumn, Summer] [Winter, Spring, Autumn, Summer] 0
⇑
Related topics
- Methods that change data in the array. Methods add(), addAll(), clear(), remove(), removeAll(), removeIf(), replaceAll(), set(), sort()
- Methods that define information about array elements. Methods get(), contains(), containsAll(), indexOf(), lastIndexOf(), iterator(), listIterator()
- Methods that determine general characteristics of the array. Methods ensureCapacity(), isEmpty(), size(), trimToSize()
- Methods that transform the array as whole. Methods clone(), sublist(), toArray(), retainAll()
⇑