Question: ///////////////Only need toArray() method to be rewritten without using the array class and it should not print any nulls after all elements are printed///////////////// ///////nothing
///////////////Only need toArray() method to be rewritten without using the array class and it should not print any nulls after all elements are printed/////////////////
///////nothing should be imported//// The method needs a array built without its class and storing that array and calling all the elements without printing null!
/////////////sortedarraycollection given if you wanna look at it//////////////////
if it works for me I will rate this !
public T[] toArray() { //return null if number is 0. //Otherwise we get the class from the first element. // could be used to create the new instance of the array, along with the size which is numElements. //Then it is easier to copy over the contents from `elements` and we return `array` if(numElements == 0) { return null; } Class clazz = elements[0].getClass(); T[] array = (T[]) Array.newInstance(clazz, numElements); for(int i = 0; i < numElements; i++) { array[i] = elements[i]; } return array; }
I need help fixing this method. it is using the sorted array collection
//---------------------------------------------------------------------------- // SortedArrayCollection.java by Dale/Joyce/Weems Chapter 5 // // Implements the CollectionInterface using a sorted array. The collection is // stored in increasing order as defined by the compareTo method of the added // elements. Only Comparable elements should be added to a collection. It is // assumed that the compareTo method and equals method as defined for added // elements are consistent. // // Null elements are not permitted in this collection. // // Two constructors are provided: one that creates a collection of a default // original capacity, and one that allows the calling program to specify the // original capacity. The collection is unbounded. //----------------------------------------------------------------------------
package Homework3;
public class SortedArrayCollection
public SortedArrayCollection() { elements = (T[]) new Object[DEFCAP]; origCap = DEFCAP; }
public SortedArrayCollection(int capacity) { elements = (T[]) new Object[capacity]; this.origCap = capacity; }
protected void enlarge() { // Create the larger array. T[] larger = (T[]) new Object[elements.length + origCap];
// Copy the contents from the smaller array into the larger array. for (int i = 0; i < numElements; i++) { larger[i] = elements[i]; }
// Reassign elements reference. elements = larger; }
protected void find(T target) { location = 0; found = false; if (!isEmpty()) recFind(target, 0, numElements - 1); }
protected void recFind(T target, int first, int last) // Used by find. { int result; // result of the comparison if (first > last) { found = false; result = ((Comparable
public boolean add(T element) { if (numElements == elements.length) enlarge();
find(element); // sets location to index where element belongs
for (int index = numElements; index > location; index--) elements[index] = elements[index - 1];
elements[location] = element; numElements++; return true; }
public boolean remove(T target) { find(target); if (found) { for (int i = location; i <= numElements - 2; i++) elements[i] = elements[i + 1]; elements[numElements - 1] = null; numElements--; } return found; }
public int size() { return numElements; }
public boolean contains(T target) { find(target); return found; }
public T get(T target) { find(target); if (found) return elements[location]; else return null; } public boolean isEmpty() { return (numElements == 0); }
public boolean isFull() { return false; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
