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

Write the add(), remove(), and get() methods which use indices . This class does

ID: 3593851 • Letter: W

Question

    Write the add(), remove(), and get() methods which use indices. This class does not (and should not) implement the List interface.     import java.util.Arrays;    * @param <E> Type of data held in this collection  */ public class ArrayList<E> {   /** First index in the array at which elements cannot be found. Only indices LESS THAN this value are valid. */   private int _size;    /** Array in which the elements are stored. */   private E[] _store;    /**    * Creates an empty list with a backing store of length 32.    */   @SuppressWarnings("unchecked")   public ArrayList() {     _size = 0;     _store = (E[]) (new Object[32]);   }    /**    * This method is used to insert the given element at the specified index. It must first check if the index is legal    * and throw an exception if it is not. It then will check if space exists in the backing store (and double the array    * length when necessary). It must then shift all of the data up to create the "hole" in which the element can be    * inserted. The last step will be to actually add the element to the backing store.    *    * @param element Data to be added to this list    * @param index Location in the array to add the element.    */   public void add(int index, E element) {    }    /**    * Remove the element that was at the specified index. To fill any "hole" created, the remaining elements should be    * shifted down from the end. It will returns the element that was removed from the list.    *    * @param index Index in the list for the element to be removed.    * @return The element that was been removed from the list.    */   public E remove(int index) {    }      /**    * Retrieve the element that was at the specified index. This will not change the elements in any way.    *    * @param index Index in the list for the element to be retrieved.    * @return The element at the specified index    */   public E get(int index) {    } } 

Explanation / Answer

package menuDriven;

import java.util.Arrays;

/* @param <E> Type of data held in this collection

*/

public class ArrayList<E> {

   /**

   * First index in the array at which elements cannot be found. Only indices

   * LESS THAN this value are valid.

   */

   private int _size;

   /** Array in which the elements are stored. */

   private E[] _store;

   /**

   * Creates an empty list with a backing store of length 32.

   */

   @SuppressWarnings("unchecked")

   public ArrayList() {

       _size = 0;

       _store = (E[]) (new Object[32]);

   }

   /**

   * This method is used to insert the given element at the specified index.

   * It must first check if the index is legal and throw an exception if it is

   * not. It then will check if space exists in the backing store (and double

   * the array length when necessary). It must then shift all of the data up

   * to create the "hole" in which the element can be inserted. The last step

   * will be to actually add the element to the backing store.

   *

   * @param element

   * Data to be added to this list

   * @param index

   * Location in the array to add the element.

   */

   public void add(int index, E element) {

       if (index >= _store.length)

           return;

       else

       {

           _size++;

           for (int x = _size - 1; x > index; x--) {

               _store[x] = _store[x - 1];

           }

           _store[index] = element;

       }

   }

   /**

   * Remove the element that was at the specified index. To fill any "hole"

   * created, the remaining elements should be shifted down from the end. It

   * will returns the element that was removed from the list.

   *

   * @param index

   * Index in the list for the element to be removed.

   * @return The element that was been removed from the list.

   */

   public E remove(int index) {

       Object e = null;

       if (index >= _size || _size == 0)

           return null;

       else {

           e = _store[index];

           for (int x = index; x < this._store.length - 1; x++) {

               _store[x] = _store[x + 1];

           }

           _size--;

       }

       return (E)e;

   }

   /**

   * Retrieve the element that was at the specified index. This will not

   * change the elements in any way.

   *

   * @param index

   * Index in the list for the element to be retrieved.

   * @return The element at the specified index

   */

   public E get(int index) {

       if (index >= _store.length)

           return null;

       else

           return _store[index];

   }

}

===============
Implemented all the methods, Thanks, let me know if ther is any concern. PLEASE RATE if you think its helpful


Try this One

import java.util.Arrays;

/* @param <E> Type of data held in this collection

*/

public class ArrayList<E> {

   /**

   * First index in the array at which elements cannot be found. Only indices

   * LESS THAN this value are valid.

   */

   private int _size;

   /** Array in which the elements are stored. */

   private E[] _store;

   /**

   * Creates an empty list with a backing store of length 32.

   */

   @SuppressWarnings("unchecked")

   public ArrayList() {

       _size = 0;

       _store = (E[]) (new Object[32]);

   }

   /**

   * This method is used to insert the given element at the specified index.

   * It must first check if the index is legal and throw an exception if it is

   * not. It then will check if space exists in the backing store (and double

   * the array length when necessary). It must then shift all of the data up

   * to create the "hole" in which the element can be inserted. The last step

   * will be to actually add the element to the backing store.

   *

   * @param element

   * Data to be added to this list

   * @param index

   * Location in the array to add the element.

   */

   public void add(int index, E element){

       if (index <0 )

           throw new IndexOutOfBoundsException();

      

       else if (_size >= _store.length){

          

           Object[] temp = new Object[2*_store.length];

           for(int i=0;i<_size;++i){

               temp[i] = _store[i];

           }

           _store = (E[]) temp;

           _store[_size++] = element;

           return;

       }

       else

       {

           _store[_size++] = element;

       }

   }

   /**

   * Remove the element that was at the specified index. To fill any "hole"

   * created, the remaining elements should be shifted down from the end. It

   * will returns the element that was removed from the list.

   *

   * @param index

   * Index in the list for the element to be removed.

   * @return The element that was been removed from the list.

   */

   public E remove(int index) {

       Object e = null;

       if (index <0 || index >= _size || _size == 0)

           return (E) new IndexOutOfBoundsException();

       else {

           e = _store[index];

           for (int x = index; x < this._store.length - 1; x++) {

               _store[x] = _store[x + 1];

           }

           _size--;

       }

       return (E) e;

   }

   /**

   * Retrieve the element that was at the specified index. This will not

   * change the elements in any way.

   *

   * @param index

   * Index in the list for the element to be retrieved.

   * @return The element at the specified index

   */

   public E get(int index) {

       if (index >= _store.length)

           return null;

       else

           return _store[index];

   }

}

====================== remove Method ===========

public E remove(int index) {

       Object e = null;
     
       if(index < 0 )
           throw new IndexOutOfBoundsException();

       else if (index >= _size || _size == 0)

           return null;

       else {

           e = _store[index];

           for (int x = index; x < this._store.length - 1; x++) {

               _store[x] = _store[x + 1];

           }

           _size--;

       }

       return (E)e;

   }