0

I'm in need of the Collections object but Processing.js keeps spitting back an error saying Collections is not defined as though it doesn't recognize it as an object. I'm trying to find the minimum value of an ArrayList by using the Collections.min function so this would be really useful.

ArrayList<int> aaa = new ArrayList<int> ();
println(aaa);
Collections<int> fff = new Collections<int> ();
println(fff);
ZekeDroid
  • 7,089
  • 5
  • 33
  • 59

1 Answers1

0

The Collections object is not a Processing API object, but an underlying Java object, and is not available to all interpreters of Processing code (because not all interpreters are based on the JVM).

If you want to find the minimum value, it's three lines of code:

int minval = aaa.get(0);
for(int v: aaa) {
  if(v < minval) { minval = v; }
}

Done, we have our minimum value. If we wrap this in a function, we can use it wherever we want:

int getMinValue(ArrayList<Integer> numberlist) {
  int minval = numberlist.get(0);
  for(int v: numberlist) {
    if(v < minval) { minval = v; }
  }
  return minval;
}
Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
  • Well I was just looking for an answer to whether or not this could be implemented because I was trying to find a way to more efficiently finding the min value. This for loop, and all foreach loops actually, take a really long time in chrome for some reason. I'm needing to find the min value of an array with 2000 points, thirty times a second, and multiplied times 12 graphs. I resorted to using dynamic programming instead but quickly noticed I also can't do this because my arrays change every time, slightly, so I'm stuck with trying something else. Thanks for the answer though! – ZekeDroid Aug 27 '13 at 17:32
  • As a programmer: no you don't O_o If you need to compute the minimum value of 2000 points 30 times a second over 12 graphs, you need to step back and ask what it is you're actually trying to achieve. At the very least, compute the new minimum value only when your points have changed. For instance, a point-change-listener that you register with each point so that when its values change it calls your listener with an updated positional value, will never run "too many times". On a secondary note: that's good, specific information, and should have been in your question =) – Mike 'Pomax' Kamermans Aug 27 '13 at 20:05
  • Uh...ok sure but just as an FYI and since we're constructively criticizing each other, turns out the max() function works on simple arrays so that turned out to be the actual answer I was looking for. Doesn't answer this particular question but it sure as hell beats having to use for loops. – ZekeDroid Aug 28 '13 at 17:53
  • true, although at 30 times a second it'll probably still slow down the sketch a lot (the .max function is implemented as the same kind of for loop that you'd run on top of an arraylist - for disclosure, I'm a Pjs dev) – Mike 'Pomax' Kamermans Aug 28 '13 at 22:44
  • Oh awesome! I've been trying really hard to speed up my animation because it gets to the point that even at 30 fps it slows down to 15 and starts to look laggy. This may be a futile request but do you have any other tips, advanced or not, that I could look out for to get more speed? I tracked down my power sink to the graph that calls on .add and .remove 16 times per second (I update this small graph once a second) and then 15 times because there are 15 canvases running at once. It's 16 because it displays 4 data lines and pops/adds the latest value to keep a full 720 point array. – ZekeDroid Aug 30 '13 at 05:48