Write a method that takes two parameters an array of integer
Write a method that takes two parameters: an array of integers and a specific \"threshold\" value. The method should return another array containing all the elements of the original array that are greater than the threshold value. For example, calling the method on the array {8, 2, 5, 9} with a threshold value of 4 should return the array {8, 5, 6, 9}. (Write your own code for all of these computations. Do not call any methods that come with Java that operate on entire arrays.)
Solution
Here is full code The method name is filter(int a[], int n)
import java.io.*;
import java.util.*;
public class Filter {
public static int[] filter(int a[],int n)
{
int j=0;
int b[] = new int[a.length];
for(int i=0;i<a.length;i++)
{
if(a[i]>n)
{
b[j]=a[i];
j++;
}
}
int c[] = new int[j];
for(int i=0;i<j;i++)
{
c[i]=b[i];
}
return c;
}
public static void main(String[] args)
{
int a[]={8,2,5,6,9};
int b[];
b=filter(a,4);
for(int i=0;i<b.length;i++)
{
System.out.println(b[i]);
}
}
}
