Question: I need help with the following code. Where there are comments on what to do is what I need help with. package apps; public class

I need help with the following code. Where there are comments on what to do is what I need help with. package apps; public class MyArrayForInt2 { private int[] nums; private int numElements=0; public MyArrayForInt2(){ nums = new int[100]; //O(1) * 1 = O(1) } public MyArrayForInt2(int size){ nums = new int[size]; //O(size) * 1 = O(size) } public MyArrayForInt2(int[] numbers){ //O(numbers.length) or O(N) nums = new int[numbers.length]; //O(numbers.length) * 1 for(int i=0; i=idx;i--) nums[i+1] = nums[i]; nums[idx] = val; numElements++; } public void add(int[] valArray) { // add all elements of valArray to the end of the array. } public void add_k(int idx, int[] valArray) { // add all elements of valArray from the specified position. // need to keeep order // eg) [10,20,30] --> add_k(1,[1,2]) makes [10,1,2,20,30] } public void remove(int val) { // remove first instance of val. No need to keep order. int index = linearSearch(val); if (index >= 0) { nums[index] = nums[numElements-1]; numElements--; } } public void removeL(int idx) { // remove the number at idx. No need to keep order. } public void remove_k(int val) { // remove first instance of val. Keep order. int index = linearSearch(val); if (index >= 0) { for(int i=index; i removeAll_k(10) makes [2,-1] // for(int i= 0; i removeRange_k(1,3) makes [10,50] } public int countOf(int val) { // return the count of val in the array int count = 0; for(int i=0; i nums[maxIdx]) // new challenger is greater maxIdx = i; return maxIdx; } private int binarySearch(int val) { int start = 0; int end = nums.length-1; int midpoint; while(start<=end) { midpoint = (start + end)/2; if (val > nums[midpoint]) start = midpoint + 1; else if (val < nums[midpoint]) end = midpoint - 1; else // val == nums[midpoint] return midpoint; } return -1; } public int search(int val) { return linearSearch(val); } private int linearSearch(int val){ // returns the index of the given value 'val'. // if 'val' is not found, returns -1 for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!