1import java.util.List; //list abstract class
2import java.util.ArrayList; //arraylist class
3
4//Object Lists
5List l = new ArrayList();
6ArrayList a = new ArrayList();
7
8//Specialized List
9List<String> l = new ArrayList<String>();
10ArrayList<Integer> a = new ArrayList<Integer>();
11//only reference data types allowed in brackets <>
12
13//Initial Capacity
14List<Double> l = new ArrayList<Double>(5);
15//list will start with a capacity of 5
16//saves allocation times
1//requires either one of these two imports, both work.
2import java.util.ArrayList;
3//--or--
4import java.util.*
5
6ArrayList<DataType> name = new ArrayList<DataType>();
7//Defining an arraylist, substitute "DataType" with an Object
8//such as Integer, Double, String, etc
9//the < > is required
10//replace "name" with your name for the arraylist
1//ArrayList
2ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.
3
4ArrayList inherits AbstractList class and implements List interface.
5ArrayList is initialized by a size, however the size can increase if collection grows or shrunk if objects are removed from the collection.
6Java ArrayList allows us to randomly access the list.
7ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases.
8Code to create a generic integer ArrayList :
9
10ArrayList<Integer> arrli = new ArrayList<Integer>();