Detecting greatest jump in value in arrayList

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);
}

1 Like

i try:

import java.util.*;
ArrayList<Float> initialList = new ArrayList<Float>();

void setup() {
  setup_array();
  println(initialList);
  check_array();
}

void setup_array() {
  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);
}

int idx = -1; 
float delta=0, max = 0;

void check_array() {
  for ( int i = 0; i < initialList.size()-1; i++ ) {
    delta = abs (initialList.get(i) - initialList.get(i+1) );
    if ( delta > max ) {
      max = delta;
      idx = i; 
    }
  }
  println("max at idx: "+idx+" val: "+initialList.get(idx)+" maxdelta: "+max);
  exit();
}

void draw() {}

console:

[91.0, 87.0, 9.0, 11.0, 23.0, 17.0]
max at idx: 1 val: 87.0 maxdelta: 78.0
2 Likes

Wow, kll, this is a great way to do it, thank you so much.
Edward

1 Like

Ah, not sure where to mark this as solved, I guess you tick the Solution box. Thank you.