Good morning,
I want to be able to detect the element with the greatest jump in value in an arrayList.
I’ve done it this way, but I wonder if there is a “more correct” and more simplified way to do it, maybe using fewer arrays to achieve the same thing? Thank you.
import java.util.*;
ArrayList<Float> initialList = new ArrayList<Float>();
ArrayList<Float> storeList1 = new ArrayList<Float>();
ArrayList<Float> storeList2 = new ArrayList<Float>();
ArrayList<Float> compareList = new ArrayList<Float>();
void setup() {
initialList.add(91.00);
initialList.add(87.00);
initialList.add(9.00);//greatest jump in value (78)
initialList.add(11.0);
initialList.add(23.0);
initialList.add(17.0);
}
void draw() {
for (int i = 1; i < initialList.size(); i++) {//removing first element
storeList1.add(initialList.get(i));
}
for (int i = 0; i <initialList.size()-1; i++) { //removing last element
storeList2.add(initialList.get(i));
}
for (int i = 0; i <initialList.size()-1; i++) { // one less element than the initialList
compareList.add( storeList2.get(i) - storeList1.get(i));
}
}
void mousePressed() {
int myIndex= compareList.indexOf(Collections.max(compareList))+1;
float jumpInValue = Collections.max(compareList);
println("Index: "+myIndex+ " Jump in Value " + jumpInValue);
}