- package com.instanceofjavaforus;
- import java.util.ArrayList;
- public class AddElementAtSpecifiedIndex {
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("a");
- arrayList.add("b");
- arrayList.add("c");
- arrayList.add("d");
- arrayList.add("f");
- arrayList.add("g");
- arrayList.add(1,"Y");
- System.out.println("ArrayList values...");
- //display elements of ArrayList
- for(int index=0; index < arrayList.size(); index++)
- System.out.println(arrayList.get(index));
- arrayList.add(2,"Z");
- System.out.println("ArrayList values...");
- //display elements of ArrayList
- for(int index=0; index < arrayList.size(); index++)
- System.out.println(arrayList.get(index));
- }
- }
- }
- OutPut:
- ArrayList values...
- a
- Y
- b
- c
- d
- f
- g
- ArrayList values...
- a
- Y
- Z
- b
- c
- d
- f
- g
No comments