Design and implement a class that uses an array to mimic the

Design and implement a class that uses an array to mimic the behavior of some of the methods in the ArrayList class. Include the following methods: Add(): Adds an element to the array. Clear(): Removes all elements from the array. Contains(): Determines if a specified item is in the array. IndexOf(): Returns the index of the first occurrence of the specified item. Insert(): Insert an element into the array at a specified index. Remove(): Removes the first occurrence of the specified item. RemoveAt(): Removes an element at the specified index. Reverse(): Reverses the order of the elements in the array. Note, as you review the methods above, some may need arguments sent to them. For example Add() may need the object sent that is added to the data structure. Write a program to test your implementation. Maximum credit awarded for solutions that are readable, modularized, follow proper naming and spacing conventions. AFTER YOU COMPLETE YOUR WORK, close the solution, zip it and then upload the zipped solution to Blackboard under Course Documents rightarrow Exam1. Print your source code and a sample run. Recall you print a sample run by capturing the output screen using the Alt + PrtScn keys to place the active screen in the clipboard. Once it\'s in the clipboard, paste the clipboard\'s contents into a Word document using Ctrl + V. If there is still an issue with printing your source from within Visual Studio, you can also copy the program statements into the clipboard and paste that into Word.

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);

   }
}

 Design and implement a class that uses an array to mimic the behavior of some of the methods in the ArrayList class. Include the following methods: Add(): Adds
 Design and implement a class that uses an array to mimic the behavior of some of the methods in the ArrayList class. Include the following methods: Add(): Adds

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site