Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Directions and example Your task is to write a method with the following signatu

ID: 3606428 • Letter: D

Question

Directions and example Your task is to write a method with the following signature: public static Stringtl removeFromArray(Stringtl arr, string toRemove) The method should return a string array that has the same contents as arr, except without any occurrences of the toRemove string. For example, if your method is called by the code below stringt] test "this", "is", "the", "example", "of", "the", "call"; string[] result = removeFromArray (test, "the"); System.out.println(Arrays.tostring (result)) it should generate the following output: Ithis, is, example, of, call] Note: Your method will be passed values for arr and toRemove by the testing program - you should not read these values in from the user inside your method. Also, you must write this method with the signature requested above in order to receive credit. You do not need to write the code that calls the method-only the method itself. Hint: Because you must specify the length of an array when you create it, you will likely need to make two loops through the input array: one to count the number of occurrences of the toRemove string so that you can create the new array with the proper size and a second to copy all of the other strings to the new array. acBook Pro

Explanation / Answer

/*find the below java code, I have written the main function also to call method*/

import java.util.Arrays;

public class myClass {

// to removeFromArray method

public static String[] removeFromArray(String[] arr,String toRemove)

{

int count = 0; // initialize count = 0, where counter is get length of new array

for(int i =0;i < arr.length;i++){

if(!arr[i].equals(toRemove)){ // if arr[i] is not same as toRemove then increment counter

count++;

}

}

String [] result = new String [count]; // initialize output string array

// copy the string in result array

int index = 0;

for(int i =0;i < arr.length;i++){

if(!arr[i].equals(toRemove)){

result[index] = arr[i]; // copy string if it is not toRemove

index++;

}

}

return result;

}

// main method

public static void main(String[] args) {

// TODO Auto-generated method stub

String[] test = {"this", "is", "the", "example", "of", "the", "call"}; // test string

String toRemove = "the"; // the word which we want to remove

String [] result = removeFromArray(test,toRemove); // function call

System.out.println(Arrays.toString(result)); // to print string array

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote