ArrayList add item code example
Example 1: insert element into arraylist java
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("foo");
arrayList.add("bar");
arrayList.add(1, "baz");
Example 2: ArrayList add(int index, E element) method in java
import java.util.ArrayList;
public class ArrayListAddMethodExample
{
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>(6);
al.add(12);
al.add(14);
al.add(16);
al.add(18);
al.add(20);
al.add(3, 15);
for(Integer number : al)
{
System.out.println("Number: " + number);
}
}
}
Example 3: java arraylist add
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("item");
System.out.println(arrayList.contains("item"));
arrayList.remove("item");