In the attached FlexArray Java class, implement a method
public int delete (int location) {
}
that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous. Make sure to handle the array empty situation. What is the time-complexity of the method, if the array size is n.
*****************************************************************************************************************************
public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int maxEntries) { capacity=maxEntries; size=0; array=new int[capacity]; } public void add(int val) { if (size<capacity) { array[size]=val; size++; } else System.out.println("Array is full, cannot add any more values"); } // add your code public int delete (int location) { } public int set(int val, int location) { if (location>=0 && location<size){ int num= get(location); array[location]=val; return num; } else throw new ArrayIndexOutOfBoundsException(); } public int get(int location) { if (location>=0 && location<size){ int num= array[location]; return num; } else throw new ArrayIndexOutOfBoundsException(); } }
public int delete (int location) {
if(size == 0){
return -1; //returns -1 as the array is empty
}
else if(location >= size){
return -1; //returns -1 as the location is invalid with respect to size of array
}
else{
int value = array[location];
for(int i=location+1;i<size;i++){
array[i-1] = array[i];
}
size--;
return value; //returns value at location in array
}
}
Time Complexity is O(n).
Get Answers For Free
Most questions answered within 1 hours.