The following program demonstrates how to Remove All Occurrences of a Number from an Array in Java.

Basically, the following program checks for the presence of the number 10 in the array. If it finds a 10, then it replaces it by 0. Also, it shifts all the zeros to the right end of the array. For example,

Input array = (2,10,1,9,8,5,10,10,3,6). So output array=(2,1,9,8,5,3,6,0,0,0).

In this program, first, we create another array of the same length as the original array. Because, initial values of the array elements are set to 0, so all elements of the new array are 0 initially. Further, we start a for loop that iterates over all the elements of the original array. If the current element of the original is not equal to 10, it assigns this element to the new array. Hence, the remaining elements of the new array remain zero towards the right end of the array.

In order to run the following program, you can remove the comments from any of the three array declaration. While, rest of the two declarations remain commented.

``````public class Exercise9
{
public static void main(String[] args) {
//int[] myarray={1, 10, 10, 2};
// int[] myarray={10, 2, 10};
int[] myarray={1, 99, 10};
System.out.println("Array Elements...");
for(int i=0;i<myarray.length;i++)
{
System.out.print(myarray[i]+"\t");
}
int[] tarr=new int[myarray.length];
int j=0;
for(int i=0;i<myarray.length;i++)
{
int x=myarray[i];
if(x!=10)
{
tarr[j]=x;
j++;
}
}
System.out.println("\nArray Elements After Removing 10s...");
for(int i=0;i<tarr.length;i++)
{
System.out.print(tarr[i]+"\t");
}
}
}``````

Output