java add item to arraylist 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: 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 3: java arraylist add
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("item");
System.out.println(arrayList.contains("item"));
arrayList.remove("item");
Example 4: arraylist insert at position
import java.util.ArrayList;
public class main{
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(0);
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(0, 100);
for(int element : list){
System.out.print(list + ", ");
}
}
}