add at index arraylist java code example

Example 1: remove arraylist index in for loop

for (int i = data.size()-1; i >= 0; i--){
    if (data.get(i).contains(element)){
            data.remove(i);
    }
 }

Example 2: ArrayList add(int index, E element) method in java

import java.util.ArrayList;
public class ArrayListAddMethodExample
{
   public static void main(String[] args)
   {
      // creating an empty ArrayList with initial capacity
      ArrayList<Integer> al = new ArrayList<Integer>(6);
      al.add(12);
      al.add(14);
      al.add(16);
      al.add(18);
      al.add(20);
      // adding element 15 at fourth position
      al.add(3, 15);
      for(Integer number : al)
      {
         System.out.println("Number: " + number);
      }
   }
}

Example 3: python list remove at index

>>> del a[2:4]
>>> a
[0, 1, 4, 5, 6, 7, 8, 9]

Example 4: arraylist adding at index

public void add(int index, E element)
// adding element 25 at third position
arrlist.add(2,25);

Example 5: arraylist add at index

arrlist.add(2,25);

Example 6: append to arraylist by index java

arrlist.add(2,25);
// .add(index, content)