Answer:
import java.lang.*; //Importation of Java Language Package
import java.util.*; //Importation of Java Utilities Package
import sun.security.util.Length; //Importation of Utility.length Package
//Which grants us access to the length of
// the array
class QuickStart { //Start of class definition
//start of array shifting function definition.
public static int[] banish (int a1[], int a2[]) {
// Start of loop through array
For(int i = 0; i < a1.length; i++){
//Start of loop through the second array
for(int j = 0; j < a2.length; j++){
if(a1[i] == a2[j]){ //A check
//if the element in the first
//is equal to the element in the second
for(int k = i; k< a1.length - 1 ; k++ ){ //This is where the
a1[k] =a1[k+1]; // next to element
//is shifted into
//the element to
//removed.
} //End of for loop that shifts the elements
a1[a1.length-1] = 0; // This is where 0 is added to the end of
// the array to make up for the removed
// array.
} //End of if Statement that checks for a match
} //End of for loop that loops through the second array
} // End of for loop that loops through the first element
return a1; //This is where the resulting array is returned to the
// main program
} //End of array shifting function definition.
/**************************Start of Main function***********************/
public static void main(String[] args)
{
int[] intArr = {42, 3, 9, 42, 42, 11, 42, 9, 42, 42, 17, 0, 8, 2222, 4, 9 , 0, 1};//Definition of the first array
int[] intArr2 = {42, 2222, 9};//Definition of second array
//This is where the returned array is displayed
System.out.println(Arrays.toString(banish (intArr, intArr2)));
}// End of Main function
}//End of Class
Step-by-step explanation:
The Programing Challenge was to remove all the elements in array2 from array1 in such a way that an element is "removed" by shifting all subsequent elements one index to the left to cover it up, placing a 0 into the last index. To find solution to this challenge we first define a function (Named banish) that will take in two array arguments.
Within that function we create a (for loop operator) which will loop through all the elements of the first array, this is done so that we can be able to compare each elements of the first array with that of the second element. Next we create another (for loop operator) within the first for loop that will loop through the second array, this is done so that each element of the first array can be compared with every element of the second array. Next using an (if statement) a check is carried out determine if an element of the first array is equal to any element of the second element, and if it is the then the next the program does is to create a for loop within the if statement, what this for loop does is to shift the element that is after the element (i.e. the element in the first array that matches the element in the second array) into the index position of that element(i.e. the element in the first array that matches the element in the second array).to replace the element.
for(int k = i; k< a1.length - 1 ; k++ ){
a1[k] =a1[k+1];
Then it continues shifting other preceding elements to fill the gap left by the previous element until it reaches the end of the array. The next thing the program does after it exist this last for loop is to shift 0 into the last position of the first array to make up for the replaced element. It then return to the first for loop in order to carry out the same process on the next element of the first array. When all the elements of the first array have been checked then the function (banish) is made to return the modified first array to the main program. At the main program the returned array from the function(banish) is displayed.