JAVA The elements of an integer valued array can be set to 0
JAVA!!!
The elements of an integer -valued array can be set to 0 (i.e., the array can be cleared) recursively as follows: An array of size 0 is already cleared; Otherwise, set the last of the uncleared elements of the array to 0, and clear the rest of the array Write a void method named clear that accepts an integer array , and the number of elements in the array and sets the elements of the array to 0.
Solution
ClearArray.java
import java.util.Arrays;
public class ClearArray{
public static void main(String[] args) {
int a[] = {1,2,5,2,1};
clear(a,5);
System.out.println(Arrays.toString(a));
}
public static void clear (int[] array, int size)
{
if(size==0){
return ;
}
else{
array[size-1] = 0;
clear(array, size-1);
}
}
}
Output:
[0, 0, 0, 0, 0]
