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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!