Design and implement a class that uses an array to mimic the
Solution
import java.util.*;
import java.util.Collections;
public class MyArrayList {
public static void main(String args[]) {
ArrayList<String> obj = new ArrayList<String>();
/*adding elements to the array list*/
obj.add(\"Ajeet\");
obj.add(\"Harry\");
obj.add(\"Chaitanya\");
obj.add(\"Steve\");
obj.add(\"Anuj\");
/* Displaying array list elements */
System.out.println(\"Add()-Currently the array list has following elements:\"+obj);
/*Add element at the given index*/
obj.add(0, \"Rahul\");
obj.add(1, \"Justin\");
System.out.println(\"Add() with index as a parameter-Current array list after adding elements at indices 0 and 1 is :\"+obj);
/*Remove elements from array list like this*/
obj.remove(\"Chaitanya\");
obj.remove(\"Harry\");
System.out.println(\"Remove()-Current array list after removing Chaitanya and Harry is:\"+obj);
/*Remove element from the given index*/
obj.remove(1);
System.out.println(\"RemoveAt()-Current array list after removal of element at index 1 is:\"+obj);
/*to find the index of a particular element*/
int pos = obj.indexOf(\"Steve\");
System.out.println(\"IndexOf()-Steve is present at the Index:\"+pos);
/*contains() method to check whethera particular element is present or not */
boolean retval = obj.contains(\"Anuj\");
if (retval == true)
{
System.out.println(\"Contains()-element \\\"Anuj\\\" is contained in the list\");
}
else
{
System.out.println(\"Contains()-element \\\"Anuj\\\" is not contained in the list\");
}
/*reverses an array list*/
Collections.reverse(obj);
System.out.println(\"Reverse()-After Reverse Order, ArrayList Contains : \" + obj);
/*clear() method*/
obj.clear();
System.out.println(\"Clear()-The array list gets cleared : \" + obj);
}
}

