The issue with Array is that they are of fixed length so if it is full you can not resize it and add any more elements to it, likewise if there are number of elements gets removed from it the memory consumption would be the same as it doesn't shrink. On the other hand ArrayList can dynamically grow and shrink after addition and removal operations.
This class is roughly equivalent to Vector, except that it is unsynchronized.The iterators returned by this class's iterator are fail-fast.
There is a list of several ArrayList tutorials at the end of this guide, refer it to gain the understanding of ArrayList class.
ArrayList Example in Java
import java.util.*; public class ArrayListExample { public static void main(String args[]) { /*Declaration of ArrayList */ ArrayList<String> al = new ArrayList<String>(); /*Elements added to the ArrayList*/ al.add("Apple"); al.add("Pear"); al.add("Banana"); al.add("PineApple"); al.add("Orange"); /* Showing ArrayList elements */ System.out.println("ArrayList contains: "+al); /*Add element at the given index*/ al.add(0, "Blackberry"); al.add(1, "Kiwi"); /*Remove elements from array list like this*/ al.remove("Pear"); al.remove("Orange"); System.out.println("Current ArrayList is: "+al); /*Remove element from the given index*/ al.remove(1); System.out.println("Updated ArrayList is:"+al); } }
Output
ArrayList contains: [Apple, Pear, Banana, PineApple, Orange] Current ArrayList is: [Blackberry, Kiwi, Apple, Banana, PineApple] Updated ArrayList is:[Blackberry, Apple, Banana, PineApple]
ArrayList Tutorials
Please find the list of ArrayList tutorials below :
ArrayList Basics
ArrayList Sorting
- Sort ArrayList in Ascending Order
- Sort ArrayList in Descending Order
- Sort ArrayList of Objects using Comparable and Comparator
ArrayList Add/Remove
- Add element to ArrayList
- Add element at Particular Index of ArrayList
- Append Collection elements to ArrayList
- Insert all the Collection elements at the Specified Position in ArrayList
- Remove element from Specified Index in ArrayList
- Remove specified element from ArrayList
ArrayList Internal Working
ArrayList Conversions
- Convert HashSet to ArrayList
- Convert Array to ArrayList
- Convert LinkedList to ArrayList
- Convert ArrayList to String Array
Differences
- ArrayList vs Array
- ArrayList vs Vector
- ArrayList vs LinkedList
- ArrayList vs HashMap
- ArrayList vs HashSet
- ArrayList vs CopyOnWriteArrayList
Other Tutorials
Reference : ArrayList Oracle doc