Question
Note that there is little about the subject in your textbook, but knowing how to code your own ArrayList class can be handy. An ArrayList
Note that there is little about the subject in your textbook, but knowing how to code your own ArrayList class can be handy. An ArrayList is a class that contains an encapsulated array. However, an ArrayList can deal with a situation where an element is added to the encapsulated array that exceeds the existing length of the array. 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 the MyArrayList 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"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started