append to arraylist java code example
Example 1: java append to arraylist at beginning
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
int element = 0;
arr.add(0, element);
Example 2: java insert into arraylist
ArrayList<Integer> str=new ArrayList<Integer>();
str.add(0);
str.add(1);
str.add(1, 11);
Example 3: 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 4: java append to list
package com.journaldev.examples;
import java.util.ArrayList;
import java.util.List;
public class ListAddExamples {
public static void main(String[] args) {
List<String> vowels = new ArrayList<>();
vowels.add("A");
vowels.add("E");
vowels.add("U");
System.out.println(vowels);
vowels.add(2, "I");
vowels.add(3, "O");
System.out.println(vowels);
}
}
Example 5: 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 6: java arraylist add
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("item");
System.out.println(arrayList.contains("item"));
arrayList.remove("item");