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

Help me complete: ArrayMultiSet constructor -- the constructor has a single para

ID: 3749483 • Letter: H

Question

Help me complete:

ArrayMultiSet constructor -- the constructor has a single parameter that is an array of generic type named initialArray. The constructor will allocate a new array with a length equal to the length of initialArray to be used by the backing store. You should then set the size of the multiset equal to the length of initialArray. Finally, assign the entries in the backing store to alias the entries in initialArray.

toArray() -- this method returns a newly allocated array containing the elements in the multiset. The array this method returns must only contain the elements in the multiset and not any nulls or other values that are stored in the backing store, but are not in the multiset.

Explanation / Answer

Please find the modified code below.

CODE

===================

import java.util.AbstractCollection;

import java.util.ArrayList;

import java.util.Iterator;

/**

* Instances of this class are used to represent a multiset -- a searchable collection in which one can add multiple

* copies of an element.

* @param Element type for this collection

*/

@SuppressWarnings({"unused", "unchecked"})

public class ArrayMultiSet<E> extends AbstractCollection<E> {

   private E[] _store;

   private int _size;

   private long _modCount;

   private static final int DEFAULT_BACKING_STORE_LENGTH = 12;

   public ArrayMultiSet() {

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

   }

   /**

   * Create a new multiset instance. The multiset's backing store will need to be allocated with a length equal to the length of {@code initialArray}.

   * The multiset's elements should be aliases for the entries in {@code initialArray}.

   * Prerequisite: {@code initialArray} CANNOT be null.

   *

   * @param initialArray Array containing the initial elements for our ArrayMultiSet.

   */

   public ArrayMultiSet(E[] initialArray) {

       _store = (E[])(new Object[initialArray.length]);

       this._size = initialArray.length;

      

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

           _store[i] = initialArray[i];

       }

   }

   @Override

   public boolean isEmpty() {

       return _size == 0;

   }

   @Override

   public int size() {

       return _size;

   }

   /**

   * Returns a newly allocated array containing the elements in the Multiset. The returned array should have a length equal to the

   * current size of the ArrayMultiSet.

   *

   * @return Newly allocated array whose entries are aliases to the elements in the Multiset.

   */

   @Override

   public E[] toArray() {

       ArrayList<E> temp = new ArrayList<>();

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

           if(_store[i] != null)

               temp.add(_store[i]);

       }

       return (E[]) temp.toArray();

   }

   @Override

   public Iterator<E> iterator() {

       // TODO Auto-generated method stub

       return null;

   }

}