1import java.util.ArrayList;
2public class ArrayListExample
3{
4 public static void main(String[] args)
5 {
6 int num = 14;
7 // declaring ArrayList with initial size num
8 ArrayList<Integer> al = new ArrayList<Integer>(num);
9 // append new element at the end of list
10 for(int a = 1; a <= num; a++)
11 {
12 al.add(a);
13 }
14 System.out.println(al);
15 // remove element at index 7
16 al.remove(7);
17 // print ArrayList after deletion
18 System.out.println(al);
19 // print elements one by one
20 for(int a = 0; a < al.size(); a++)
21 {
22 System.out.print(al.get(a) + " ");
23 }
24 }
25}
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
1import java.util.ArrayList;
2ArrayList<Integer> myList = new ArrayList<Integer>();
3myList.add(0);
4myList.remove(0);//Remove at index 0
5myList.size();
6myList.get(0);//Return element at index 0