In JAVA please :Write a method called removeInRange that accepts four parameters: an ArrayList of integers, an element value, a starting index, and an ending index. The method's behavior is to remove all occurrences of the given element that appear in the list between the starting index (inclusive) and the ending index (exclusive). Other values and occurrences of the given value that appear outside the given index range are not affected. For example, for the list [0, 0, 2, 0, 4, 0, 6, 0, 8, 0, 10, 0, 12, 0, 14, 0, 16], a call of removeInRange(list, 0, 5, 13); should produce the list [0, 0, 2, 0, 4, 6, 8, 10, 12, 0, 14, 0, 16]. Notice that the zeros located at indices between 5 inclusive and 13 exclusive in the original list (before any modifications were made) have been removed.

Respuesta :

Answer:

public static void removeInRange(ArrayList<Integer> myList, int value, int start, int end){

   int count = 0;

   int i = start;

   while( i < (end-count) ) {

       if( myList.get(i) == value ) {

           myList.remove(i);

           count++;

       }

       else {  

           i++;

       }

   }

}

Explanation:

Create a method removeInRange that has 4 parameters;

  1. The list that need to be filtered
  2. The value that need to be removed from a specific range
  3. The starting index of specific range
  4. The ending index of specific range

Initialize the count variable with 0 while the i variable with starting index. Run a while loop starting from i until the value of i is less than the ending index take away count.

Inside the while loop, check if the current number is the specific value that needs to be removed. If true remove the element from the list and increment count variable. Otherwise just increment the i variable which is acting as a counter for the loop.

Keep on iterating until all specific elements are removed from the desired range.

ACCESS MORE