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
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
1ArrayList<String> languages = new ArrayList<String>();
2languages.add("PHP");
3languages.add("JAVA");
4languages.add("C#");
5ArrayList<int> numbers = new ArrayList<int>();
6numbers.add(9);
7numbers.add(14);
8numbers.add(2);
1// Assign to list variable directly with STREAM FILTER METHID
2ArrayList<String> filteredColors = (ArrayList<String>) colors.stream().filter(x -> x.contains("o")).collect(Collectors.toList());
3
4// Clone and remove unwanted(TRADITIONAL FILTERING)
5filteredColors = (ArrayList<String>) colors.clone();
6filteredColors.removeIf(x -> !x.contains("o"));
7
8// Find specific element
9colors.stream().filter(x -> x.contains("ora")).findFirst().get();