Subset on Array of Integers
The java program finds an integer subset by searching on array of integers based on condition and a value.
The class Data4 defined the following three member function for search on dataset,integer arrays. they are,
greater function The function greater is invoked by object of Data4 with one integer argument and returns list (array) of integer which are greater than given argument value.
lesser function The function greater is invoked by object of Data4 with one integer argument and returns list (array) of integer which are lesser than given argument value.
range function The function greater invoked is by object of Data4 with two integer arguments. The second argument is greater than first one. The function returns list of integers which are greater than the first argument value and lesser than the second argument value.
Output
The class Data4 defined the following three member function for search on dataset,integer arrays. they are,
1. greater 2. lesser 3. rangeand Object of class Data4 is initialized with 20 randomly integers generated from random object with limit range of value [0-1000].
greater function The function greater is invoked by object of Data4 with one integer argument and returns list (array) of integer which are greater than given argument value.
lesser function The function greater is invoked by object of Data4 with one integer argument and returns list (array) of integer which are lesser than given argument value.
range function The function greater invoked is by object of Data4 with two integer arguments. The second argument is greater than first one. The function returns list of integers which are greater than the first argument value and lesser than the second argument value.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Random;
import java.util.stream.IntStream;
public class Data4 {
int elem[];
public Data4(int elem[]) {
this.elem = elem;
}
public int[] list2int(ArrayList<Integer> list) {
int gval[] =new int[list.size()];
int k=0;
for (Integer e : list)
gval[k++] = e.intValue();
return gval;
}
public int[] greater(int gvalue)
{
ArrayList <Integer>glst = new ArrayList<Integer>();
for(int n=0;n<elem.length;n++)
{
if ( elem[n] > gvalue ) {
glst.add(elem[n]);
}
}
return list2int(glst);
}
public int[] lesser(int lvalue)
{
ArrayList <Integer>lst = new ArrayList<Integer>();
for(int n=0;n<elem.length;n++)
{
if ( elem[n] < lvalue ) {
lst.add(elem[n]);
}
}
return list2int(lst);
}
public int[] range(int lvalue,int gvalue)
{
ArrayList <Integer>lst = new ArrayList<Integer>();
for(int n=0;n<elem.length;n++)
{
if ( elem[n] >= lvalue && elem[n]<gvalue ) {
lst.add(elem[n]);
}
}
return list2int(lst);
}
public static void main(String[] args)
{
int dsize=20;
int ds[] =new int[dsize];
Random rd =new Random();
rd.setSeed(0);
for(int n=0; n<dsize ; n++)
ds[n] = rd .nextInt(1000);
Data4 data = new Data4(ds);
int gval[] = data.greater(300);
int lval[] = data.lesser(300);
int rval[] = data.range(300,500);
System.out.println("\nFind values Data array");
System.out.println("Random Data :" + Arrays.toString(ds));
System.out.println("\nFind values greater that (>300)
in Data array");
System.out.println(Arrays.toString(gval));
System.out.println("\nFind values lesser than (<300)
in Data array");
System.out.println( Arrays.toString(lval));
System.out.println("\nFind values (300 > value < 500)
in Data array");
System.out.println( Arrays.toString(rval) );
}
}
Output
Find values Data array Random Data :[360, 948, 29, 447, 515, 53, 491, 761, 719, 854, 77, 677, 473, 262, 95, 844, 84, 875, 241, 320] Find values greater that (>300) in Data array [360, 948, 447, 515, 491, 761, 719, 854, 677, 473, 844, 875, 320] Find values lesser than (<300) in Data array [29, 53, 77, 262, 95, 84, 241] Find values (300 > value < 500) in Data array [360, 447, 491, 473, 320]
Comments
Post a Comment