put value in arraylist of a class in java code example

Example 1: 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 al = new ArrayList(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 2: arraylist adding at index

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

Tags:

Misc Example