1ArrayList<Integer> integerArray = new ArrayList<Integer>();
2ArrayList<String> integerArray = new ArrayList<String>();
3ArrayList<Double> integerArray = new ArrayList<Double>();
4//... keep replacing what is inside the <> with the appropriate
5//data type
6
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
1List<String> expenseTypeList = new ArrayList<>();
2 expenseTypeList.add("GENERAL");
3 expenseTypeList.add("PURCHASE");
4 expenseTypeList.add("PROJECT");
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>();
1import java.util.ArrayList;
2ArrayList<String> languages = new ArrayList<String>(); // ArrayList of type string
3ArrayList<int> numbers = new ArrayList<int>(); // ArrayList of type int
4