Question

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);
Était-ce utile?

La solution

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top