We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I am looking for a way to sort and store a lot of float numbers in different categories. I want to be able to append numbers to any category at any time. I know how to create a 2D float array, but not how to create and access an array of FloatLists. All help appreciated!
Answers
FloatList[] array = new FloatList[number_of_elements];
A more complete example. Including populate, sort styles and even append for a FloatList[]: o->
To access static methods from another class means they have to be prepended with the class name and in Processing that is the name of the sketch. Which means the code breaks if you use File | Save As
I can't see a good to declare the methods static in this example?
And I can't see how a simple
static
would break any "File | Save As" operation! Is that seriously true? 8-}For me,
static
just means that a class member isn't bound to other instance members!And that it's merely a utility method which get everything from its parameters or it's some constant field.
And that I can move it to any other class, as long as everything it needs is properly imported too!
For example, all those 5 utility
static
methods depend on Processing's FloatList class.If I wish to copy & paste them somewhere else, I'd need
import processing.data.FloatList;
there too.AFAIK, that's the only demand in order to save them to some ".java" file! 8-|
Well, at least,
static
opens up that possibility! As long as we know the name of its 1st ".pde" tab! :))But that is true for any regular class too! In order to access 1, we gotta know its ".java" file name after all! 8-X
In short, I only plaster
static
on class members which somewhat work independent from it! >-)If some1 doesn't like them, it's enough to use an editor's replace feature for all
static
keywords! O:-)I thought I made my meaning clear but just to clarify...
If you have a Processing sketch with static methods in the pde tab and you want to use them from another class (java tab) then they have to be prepended with the name of the sketch. If some time later you rename the sketch with File | Save As then the sketch will not run.
In a Processing sketch without java tabs then there is no need or benefit to using the static keyword (#) and for a newbie it just introduces a new conundrum for the OP to puzzle over.
(#) not quite true I can think of one reason to use the static keyword but a novice is unlikely to need it.
True - but then the person using the methods must know enough that, in this case, they can safely remove the static keyword. It is more likely that a newbie would assume they are required.
Apart from that I am sure the OP will find your methods useful :)
IMO, if a Processing programmer knows how to make a ".java" tab; and even more spectacularly, knows how to convert Processing's syntax sugar to proper Java, he's at least intermediary, not a Java rookie! >:/
And I'd be impressed if a regular Processing's programmer knew that we could access sketch's
static
members from a ".java" file's top-class by using the 1st ".pde" tab's name as a class type via the dot.
operator! O:-)It's unknown to me whether Processing's site even teaches that all ".pde" files are merged as 1 to become a full-fledged Java top-class. I've yet to see any mention there's a pre-processor to transpile its syntax sugar to Java!
They don't even mention that our classes are a special case called nested inner class!
Your worries seem overrated to me I'm afraid! Looking ahead for a very improbable ".pde" file rename bug case! :-\"
It's as if they'd be tempting us: "I'm here in a ".pde" file! Plz use me from inside this remotely ".java" expert tab!" :D
Here is my take on GoToLoop's excellent example
Since you've removed all
static
, among other things too, no need for a plain Math.random() anymore!You can go back to Processing's own
float
& non-static
random() version now: :ar!Thank y’all for your contributions! Here is a compressed code example that might help other novices in the same situation:
Here's the same snippet, but calling the original populateCategories(): (*)