Ask Question

Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.

+2
Answers (1)
  1. 11 April, 02:20
    0
    Since the question expects us to write a method to remove an item from array, we can presume the array built-in method such as removeItem () is not applicable in this context (as this will spoil the purpose of this exercise). Based on this presumption, a method to remove item from array can be as follows (written in Java):

    public static int[] remove (int [] arr, int l, int removeItem) { if (l = = 0) { System. out. println ("The array is empty!"); return arr; } int indexFound = - 1; for (int i = 0; i < l; i++) { if (arr[i] = = removeItem) { indexFound = i; } } if (indexFound = = - 1) { System. out. println ("The remove item is not found in the array"); return arr; }else{ int newArray[] = new int[l-1]; int j = 0; for (int i=0; i < l; i++) { if (i = = indexFound) { continue; } newArray[j] = arr[i]; j++; } return newArray; } } }

    Explanation:

    We start by defining a method called remove () with three expected input, array (arr), length of array (l) and item to be removed (removeItem).

    If the input array is empty (which means the length of array is 0), just print out a message "The array is empty!" and return the original array.

    If not, the program will proceed to check if the removeItem is found in the array by using a for-loop to traverse through each of the number in the array. (Line 10 - 14) If the removeItem is found, we can track the index where the item is found using a variable, indexFound. (Line 12)

    If the removeItem is not found in the array, the initial value of the indexFound will remain - 1 and this can be set as a flag to print out a message "The remove item is not found in the array". (Line 17)

    If found, the program will proceed to create a new array, newArray and copy the values from the original array, arr, to it with the exception of the remove item (Line 20 - 28). The reason we need to create a new array is because Java doesn't allow us to change the size of array after it has been initialized. Therefore, we need to develop an alternative mechanism that copy the values from the original array to the new array.

    At the end, the new array with size reduced by 1 is returned as an output (Line 29).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers