Question
Use JAVA language. public class DynamicArray2 { private String[] data; // the backing array private int virtualArrayLength; // the number of elements in the dynamic
Use JAVA language.
public class DynamicArray2 {
private String[] data; // the backing array
private int virtualArrayLength; // the number of elements in the dynamic array
// Throws an IndexOutOfBoundsException if i is not a valid index
// for adding to the dynamic array, otherwise inserts s at index i.
// Elements can be added from index 0 to this.size().
public void add(int i, String s) {
// If there is no room for s in data, create a new array
// that is twice as long as data. Copy the contents of data
// over to this new array. Set data (the reference to the
// backing array) to this new array.
// Shift the items in data at indexes starting at i up by one,
// to make room for s at index i.
// HINT: Try this on paper first
// Add s at index i.
// Update virtualArrayLength.
// (DO NOT create a new array each time this method is called.
// Do so ONLY when the capacity of data in the backing store is exceeded,
// which will happen infrequently.)
}
// Throws an IndexOutOfBoundsException if i is not a valid index
// of the dynamic array, otherwise removes the element at index i
// and shifts the elements after i down one to fill in the gap.
public void remove(int i) {
// DO NOT create a new array anywhere in this method.
// Instead, shift the items in data from indexes i+1 on
// down by one, to overwrite the "removed" string at index i.
}
}
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