Answered step by step
Verified Expert Solution
Link Copied!

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 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"
*You may want to review the discussion of the ArrayList collection class provided by the Java API, as found on pages 380-399(chapter 12) of the Murach Java textbook currently used in MCC's Java Programming I & II courses.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro PowerShell For Database Developers

Authors: Bryan P Cafferky

1st Edition

1484205413, 9781484205419

More Books

Students also viewed these Databases questions

Question

6.2 Explain the recruitment process.

Answered: 1 week ago