Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use JAVA language. public class DynamicArray2 { private String[] data; // the ba

ID: 3730286 • Letter: U

Question

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.

}

}

Explanation / Answer

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.)

      

       if(data.length == virtualArrayLength) {

           String[] newArr = new String[virtualArrayLength*2];

           for(int j=0; j<virtualArrayLength; j++)

               newArr[i] = data[i];

          

           data = newArr;

       }

      

       int j = virtualArrayLength;

       while(j > i) {

           data[virtualArrayLength] = data[i-1];

           j--;

       }

      

       virtualArrayLength++;

       data[j] = s;

   }

   // 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.

       if( i < 0 || i >= virtualArrayLength)

           throw new IndexOutOfBoundsException();

      

       for(int j=i; j<virtualArrayLength-1; j++)

           data[j] = data[j+1];

      

       virtualArrayLength--;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote