Within Java. Create an ExpandableArray class. There should be methods add, get,
ID: 648946 • Letter: W
Question
Within Java. Create an ExpandableArray class. There should be methods add, get, set, and size. The methods should work like the identically named methods in an ArrayList. Internally the ExpandableArray should have an int array with two slots initally. The size method should return the net number of integers added- not the number of slots in the internal array. Initially size should be zero. If on a call of add the internal array is full, the add method should:
a) Create a new array with twice the number of slots as the current array.
b)Copy the contents of the original array to the new array, and thereafter use the new array until it is not big enough
Write a program that uses the ExpandableArray class it should:
1)Create an ExpandableArray object.
2)Add 1,2,99,4,6,7,8,9, and 10, in that order
3)Set 99 to 3.
4)Insert 5 after the number 4
5)Display the current size
6)Display the integers stored by calling get within a for loop
Explanation / Answer
/** * This class provides methods for working with an array that expands * to include any positive index value supplied by the caller. */ public class ExpandableArray { /** * Creates a new expandable array with no elements. */ public ExpandableArray() { ExpandableArray = new Object[0]; } /** * Sets the element at the given index position to the specified. * value. If the internal array is not large enough to contain that * element, the implementation expands the array to make room. */ public void set(int index, Object value) { if (index >= ExpandableArray.length) { // had index > length-1 == same thing Object[] newArray = new Object[index+1]; //needs plus one because it's the length for (int i = 0; i = ExpandableArray.length) return null; return ExpandableArray[index]; } private Object[] ExpandableArray; }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.