Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Note that there is little about the subject in your textbook, but knowing how to code your own ArrayList class can be handy. * An
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 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 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 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; creates an encapsulated array with elements within the MyArrayList object.
list.addAlpha; the array contains Alphanullnullnullnull
list.addBeta; the array contains AlphaBetanullnullnull
list.addGamma; the array contains AlphaBetaGammanullnull
list.addDelta; the array contains AlphaBetaGammaDeltanull
list.addEpsilon; the array contains AlphaBetaGammaDeltaEpsilon
list.addAlpha; the array contains AlphaBetaGammaDeltaEpsilonAlphanullnullnullnull
System.out.printlnlistsize; displays ;
System.out.printlnlistsearchAlpha; displays
System.out.printlnlistsearchFromEndAlpha; displays
System.out.printlnlistfindMatchesAlpha; displays
int arr list.findMatchesArrayAlpha;
forint x: arr
System.out.printlnx;
the above displays and
System.out.printlnlistget; displays "Gamma"
You may want to review the discussion of the ArrayList collection class provided by the Java API, as found on pages chapter of the Murach Java textbook currently used in MCCs Java Programming I & II courses.
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