how to add an element to an arraylist java code example
Example 1: arraylist in java
import java.util.ArrayList;
public class ArrayListExample
{
public static void main(String[] args)
{
int num = 14;
ArrayList<Integer> al = new ArrayList<Integer>(num);
for(int a = 1; a <= num; a++)
{
al.add(a);
}
System.out.println(al);
al.remove(7);
System.out.println(al);
for(int a = 0; a < al.size(); a++)
{
System.out.print(al.get(a) + " ");
}
}
}
Example 2: java arraylist
List<String> expenseTypeList = new ArrayList<>();
expenseTypeList.add("GENERAL");
expenseTypeList.add("PURCHASE");
expenseTypeList.add("PROJECT");
Example 3: how to add to an arraylist java
import java.util.ArrayList;
public class Details {
public static void main(String[] args) {
ArrayList<String> al= new ArrayList<String>();
al.add("Ram");
al.add("Shyam");
al.add("CPS");
al.add("John");
al.add("Steve");
System.out.println("Elements of ArrayList of String Type: "+al);
Example 4: java arraylist add
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("item");
System.out.println(arrayList.contains("item"));
arrayList.remove("item");