Create a class called MyArrayList It should have the followi

Create a class called MyArrayList. It should have the following members:

An encapsulated String array.

A constructor that sets the size of the array.

An \"add\" method that takes a String argument and adds it to the last free element in the array. However, if the array is full, the add method creates a new array with double the elements of the existing array. The contents of the old array are copied to the new array and the String argument is assigned to the next free element of the new array. The new array is then assigned to the instance variable of the encapsulated array.

A \"get\" method that takes an index number argument and returns the String object from that index position. If the index number is out of the existing number range of the array, it should return a null.

A method called \"size\" that returns an integer that is the number of elements in the array.

A \"search\" method that takes a String argument and searches the array from the beginning (index 0) to the last element in the array. If the method finds a match for the search argument, it returns the index number of the match. If it doesn\'t find a match, it returns a -1. Don\'t worry about dealing with repeated values within the array -- just return the first position number found.

A \"searchFromEnd\" method that takes a String argument and searches the array from the last index to index 0. If it finds a match, return the index position. Again, don\'t worry about dealing with repeated values within the array -- just return the first position number found.

A \"findMatches\" method that takes a String argument and searches the array. It will return a number that is how many matches were found for the String argument in the array.

A \"findMatchesArray\" method that takes a String argument and searches the array. It will return an int array that has the index numbers for the matches found in the String array. If it finds no matches, it returns a null.
Example of how your driver code might look:

MyArrayList list = new MyArrayList(5); //creates an encapsulated array with 5 elements within theMyArrayList object.
list.add(\"Alpha\"); //the array contains [\"Alpha\"][null][null][null][null]
list.add(\"Beta\"); //the array contains [\"Alpha\"][\"Beta\"][null][null][null]
list.add(\"Gamma\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][null][null]
list.add(\"Delta\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][\"Delta\"][null]
list.add(\"Epsilon\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][\"Delta\"][\"Epsilon\"]
list.add(\"Alpha\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][\"Delta\"][\"Epsilon\"][\"Alpha\"][null][null][null][null]
System.out.println(list.size()); //displays \"10\";
System.out.println(list.search(\"Alpha\")); //displays \"0\".
System.out.println(list.searchFromEnd(\"Alpha\")); //displays \"5\".
System.out.println(list.findMatches(\"Alpha\")); //displays \"2\".
int[] arr = list.findMatchesArray(\"Alpha\"));
for(int x: arr){
System.out.println(x);
}
// the above displays \"0\" and \"5\".
System.out.println(list.get(2)); //displays \"Gamma\"

Solution


public class MyArrayTester {

   public static void main(String[] args) {

       MyArrayList list = new MyArrayList(5);
       //creates an encapsulated array with
       //5 elements within theMyArrayList object.
       list.add(\"Alpha\"); //the array contains [\"Alpha\"][null][null][null][null]
       list.add(\"Beta\"); //the array contains [\"Alpha\"][\"Beta\"][null][null][null]
       list.add(\"Gamma\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][null][null]
       list.add(\"Delta\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][\"Delta\"][null]
       list.add(\"Epsilon\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][\"Delta\"][\"Epsilon\"]
       list.add(\"Alpha\"); //the array contains [\"Alpha\"][\"Beta\"][\"Gamma\"][\"Delta\"][\"Epsilon\"][\"Alpha\"][null][null][null][null]
      
       System.out.println(\"List elemnets\");
       System.out.println(list);
      
       System.out.println(\"Size of list\");
       System.out.println(list.size()); //displays \"10\";
       System.out.println(\"Finding position of Alpha\");
       System.out.println(list.search(\"Alpha\")); //displays \"0\".
       System.out.println(\"Finding position of Alpha from end\");
       System.out.println(list.searchFromEnd(\"Alpha\")); //displays \"5\".
       System.out.println(\"Finding number of matches of Alpha\");
       System.out.println(list.findMatches(\"Alpha\")); //displays \"2\".
              
       int[] arr = list.findMatchesArray(\"Alpha\");
       System.out.println(\"Position of Alpha matching in list\");
       for(int x: arr){
           System.out.println(x);
       }
       // the above displays \"0\" and \"5\".
       System.out.println(\"Get value at index 2\");
       System.out.println(list.get(2)); //displays \"Gamma\"

   }

}

---------------------------------------------------------------------------------------------------------------


//MyArrayList.java
import java.util.ArrayList;
public class MyArrayList {
   private String[] myElements;
   private int mySize;

  
   //constructor that takes size
   public MyArrayList(int size) {
       //create an arry of tyoe string of size
       myElements = new String[size];
       mySize = 0;
   }

   // adds given element at the end
   public void add(String element) {
       add(size(), element);
   }
  
   //Retunrs the search index of the key value in list
   public int search(String key){

       int posIndex=-1;
       boolean found=false;
       for (int i = 0; i < size() && !found; i++)
       {
           if(myElements[i].equals(key))
           {
               posIndex=i;
               found=true;
           }
       }
       return posIndex;
   }
  
   //Returns the integer list of the matching key in the list
   public int[] findMatchesArray(String key){

       ArrayList<Integer>matchList=new ArrayList<Integer>();
       int posIndex=-1;
       boolean found=false;
       for (int i = 0; i < size() && !found; i++)
       {
           if(myElements[i].equals(key))
           {
               posIndex=i;
               matchList.add(posIndex);
           }
       }      
       int[] list=new int[matchList.size()];      
       for (int i = 0; i < matchList.size(); i++) {
           list[i]=matchList.get(i);
       }
      
       return list;
   }
  
   //Search key from end of the list
   public int searchFromEnd(String key){

       int posIndex=-1;
       boolean found=false;
       for (int i = myElements.length-1; i >=0 && !found; i--)
       {
           if(myElements[i]!=null &&myElements[i].equals(key))
           {
               posIndex=i;
               found=true;
           }
       }
       return posIndex;
   }
  
  
   //Retunrs the number of the mathche of key
   public int findMatches(String key){

       int matches=0;
       for (int i = 0; i < size(); i++)
       {
           if(myElements[i].equals(key))
           {
               matches++;
           }
       }
       return matches;
   }

   //resize the array by double
   private void resize() {
       String[] newArray = new String[size() * 2];
       System.arraycopy(myElements, 0, newArray, 0, size());
       myElements = newArray;
   }

  
  
   //Get string value at index,i
   public String get(int i) {  
       if(i<0 || i>mySize)
           throw new IllegalAccessError();
       else
           return myElements[i];
   }


   // Adds given element at the given index
   private void add(int index, String element) {
       if (index < 0 || index > size())
           throw new IndexOutOfBoundsException();

       if (size() == myElements.length) {
           resize();
       }
       //move elements
       for (int i = size(); i > index; i--)
           myElements[i] = myElements[i - 1];

       // add element
       myElements[index] = element;

       mySize++;
   }

   // removes all elements
   public void clear() {
       mySize = 0;
   }

  
   // returns true if there are no elements in the list
   public boolean isEmpty() {
       return mySize == 0;
   }

   // returns the number of elements in this list
   public int size() {
       return mySize;
   }

   // returns this list as an array of objects
   public Object[] toArray() {
       Object[] array = new Object[size()];
       System.arraycopy(myElements, 0, array, 0, size());
       return array;
   }

   // returns this list as a string, such as \"[12, 5, -7, 3]\"
   public String toString() {
       String result = \"\";
       for (int i = 0; i < size(); i++) {
           result += myElements[i];
           if (i < size() - 1)
               result += \", \";
       }

       return \"[\" + result + \"]\";
   }


  

}

---------------------------------------------------------------------------------------------------------------

Sample output:

List elemnets
[Alpha, Beta, Gamma, Delta, Epsilon, Alpha]
Size of list
6
Finding position of Alpha
0
Finding position of Alpha from end
5
Finding number of matches of Alpha
2
Position of Alpha matching in list
0
5
Get value at index 2
Gamma

Create a class called MyArrayList. It should have the following members: An encapsulated String array. A constructor that sets the size of the array. An \
Create a class called MyArrayList. It should have the following members: An encapsulated String array. A constructor that sets the size of the array. An \
Create a class called MyArrayList. It should have the following members: An encapsulated String array. A constructor that sets the size of the array. An \
Create a class called MyArrayList. It should have the following members: An encapsulated String array. A constructor that sets the size of the array. An \
Create a class called MyArrayList. It should have the following members: An encapsulated String array. A constructor that sets the size of the array. An \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site