Giving an arraylist say [3,10,9,11,10,2,9], i need to write some code to pick out the sequence [10,9,11,10].
The way this have to be done is by creating a interval from an element and then check whether the next element(s) is in that interval.
fx.:
From the element 3 we get the interval, 3*0,80 and 3*1,20 = [2.4 – 3.6]. Now checking if the next element, 10, is in that interval – its not. Move to next element, 10.
From 10 we get the interval [8 – 12]. We see that 9, 11 and 10 is in that interval, 2 is not so stop!
What i have:
import java.util.ArrayList;
import java.util.List;
public class HelloWorld {
public static void main (String[] args){
List<Integer> tops = new ArrayList<Integer>();
tops.add(3);
tops.add(10);
tops.add(9);
tops.add(11);
tops.add(10);
tops.add(2);
tops.add(9);
List<Integer> newtops = new ArrayList<Integer>();
for (int i = 1; i < tops.size()-1; i++){
double minValue = tops.get(i)*0.80;
double maxValue = tops.get(i)*1.20;
for (int k = i+1; k < tops.size()-1; k++){
if (tops.get(i) > minValue && tops.get(i) < maxValue){
newtops.add(tops.get(i));
}
else{
break;
}
}
}System.out.println(newtops);
}
}
Output:
[10, 10, 10, 10, 9, 9, 9, 11, 11, 10]
Expected output:
[10, 9, 11, 10]
I think this will work. Untested…