We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › object oriented question
Page Index Toggle Pages: 1
object oriented question (Read 1072 times)
object oriented question
Aug 5th, 2008, 5:59pm
 
Is there a way to have an array of all the objects of a specific class?  Even better if it could be sorted based on one of their variables.  It seems like there should be something like this, as it would greatly simplify many of my programming tasks.  I would just create the array manually, but I can't even figure out how to create an array of objects in processing!  Any help would be appreciated.

Thanks,
Zach
Re: object oriented question
Reply #1 - Aug 5th, 2008, 6:02pm
 
You can create an array of objects easily:

Code:
MyObj[] thing=new MyObj[10];
//an array of 10 MyObj-es


As for sorting, if your class implements the Comparable interface, to do sorting, you can use Arrays.sort(thing); where thing is the array of 10 MyObj-es
Re: object oriented question
Reply #2 - Aug 5th, 2008, 6:28pm
 
Thanks.  It would solve my problem just to construct the objects in an array, rather than trying to place them in one afterwards.  

However, it also seems like there should be some datatype that represents the "universe" of objects of a given class.  I've never seen something like this in any language, but it would be helpful.  

Can you link me to some examples of the Comparable interface in processing?  I can't find anything on that either.  
Re: object oriented question
Reply #3 - Aug 5th, 2008, 7:02pm
 
Unfortunately there's no magic "find every Foo object that exists in the code and put it into an array" sorry, you have to do that yourself.

The Comparable interface is a Java thing, but you can use all Java stuff in processing. You'd go about using it like:

Code:
class MyObj implements Comprabale
{
int x,y;
MyObj(...)
// normal stuff that you use anyway...

//if we want to sort based on the X value of MyObj-es:
int compareTo(Object o)
{
MyObj other=(MyObj)o;
if(other.x>x)
return -1;
if(other.x==x)
return 0;
else
return 1;
}
}


IF you create an array of MyObj-es then call Arrays.sort(theMyObjArray); it'll be sorted based on the X value in each MyObj.
Re: object oriented question
Reply #4 - Aug 6th, 2008, 5:53am
 
hi john, i added your comparable interface example to the hacks if you dont mind (link), feel free to edit. i thought it might be useful there.
best,
andi
Re: object oriented question
Reply #5 - Aug 6th, 2008, 6:44am
 
In truth, Processing does not provide a language in a true sense - it provides a preprocessor that takes line-level valid Java code you input as "Processing code" into the Processing Development Environment (PDE), embeds it in code for Java classes, compiles the assembled Java files, and executes the resultant byte code. Therefore, you pick up all the benefits and limitations of Java as a language and runtime (Java Virtual Machine) environment.

Therefore, when you say that Processing does not have a way to find all instances of class Foo, you really mean Java. Java does not have a built-in "object space" accessible via the standard library that allows you to find all instances of a class, but many other languages like Smalltalk and Ruby do. For example:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ospace.html

To roughly approximate using Java, you can, instead of creating an object like so:

Foo foo = new Foo();

Create a static class method to create new Foo instances:

Foo foo = Foo.instance();

Implement Foo.instance() to construct a new instance, add a reference to the new instance in a class variable collection, and then return the new instance. Then implement a:

Collection foos = Foo.instances();

Static class method that returns the Collection of Foos kept in the class variable (maintain care if modifying the returned collection - you may want to clone the collection).

Of course, you need to analyze whether you really need this at the class level. Often, a design that necessitates this indicates an overall application design flaw and will reduce to a simpler pattern.
Re: object oriented question
Reply #6 - Aug 6th, 2008, 12:55pm
 
I think you can do this with introspection (look at the Class class!) and/or perhaps the debug tools.
But as pointed out, it is a bit stretched, there no real need to do that, since you can put the created objects in an ArrayList (for example) as soon as they are created. More readable, better design and probably more efficient.
Re: object oriented question
Reply #7 - Aug 6th, 2008, 5:15pm
 
Thanks, smith.  I am indeed simplifying the design to avoid needing the object space, but this has been a pretty fascinating discussion for me.  I have never tried to learn Smalltalk or Ruby and wasn't aware that any language implements such a thing.  Definitely something to think about as I get more into OO design.
Re: object oriented question
Reply #8 - Aug 8th, 2008, 1:46am
 
If you do track all instances of a class, I recommend HashSet over ArrayList for the Collection implementation:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/HashSet.html

Otherwise, you can add a single instance to the collection multiple times. ArrayList will not enforce uniqueness of each member at a data structure level; HashSet will.

If you override the Object.equals(Object obj) method on your class, you must read the Java documentation on Object.equals(Object obj) and Object.hashCode() in full.
Page Index Toggle Pages: 1