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 & HelpIntegration › How to use InstanceOf
Page Index Toggle Pages: 1
How to use InstanceOf? (Read 3594 times)
How to use InstanceOf?
May 20th, 2006, 10:51pm
 
Hi everyone, Help is needed!

I have several different objects piled in an ArrayList and I need to loop through the list and find objects of a specific class. I found the "isInstanceOf" and the "getInstanceOf", but I can't find a way to integrate them in Processing and there is no help to be found in the archive.

I need a simple example.

Thanks for your time Wink
~Carl Email
http://moodplug.com
Re: How to use InstanceOf?
Reply #1 - May 20th, 2006, 11:25pm
 
ArrayList list = new ArrayList();

list.add("string1");
list.add("string2");
list.add(new Integer(1));
list.add(new Integer(2));

for (Iterator it=list.iterator(); it.hasNext();) {
Object obj = it.next();
if (obj instanceof String)
  println(obj);
}

// prints:
// string1
// string2

Re: How to use InstanceOf?
Reply #2 - May 22nd, 2006, 12:55pm
 
jkriss is right, that's how to do it.  BUT... there's probably a better way Smiley

In your case, you don't say what you're doing with the instances once you've identified them, so I can't be sure what's best.

One thing you might try is having two ArrayLists, or more.  e.g. you might have one called chairs, one called tables, and one called furniture which has everything from chairs and everything from tables.  Then when you only want to mess with the chairs you don't need to loop through all the furniture.

Another better way might be to have a common 'base class' for all the things in your ArrayList, and to provide a method on each class which does something different.

e.g. instead of this:

Code:


class Cow {
// things to do with Cows go here
}

class Sheep {
// things to do with Sheep go here
}

ArrayList list = new ArrayList();

list.add(new Cow());
list.add(new Cow());
list.add(new Sheep());
list.add(new Sheep());

Iterator it = list.iterator();
while (it.hasNext()) {
Object obj = it.next();
if (obj instanceof Cow) {
println("moo");
}
else {
println("baa");
}
}

// prints:
// moo
// moo
// baa
// baa




It's much better to do something like this:

Code:


class Animal {
void talk() {
println("nothing special");
}
}

class Cow extends Animal {
// Cows have something special to say:
void talk() {
println("moo");
}
}

class Sheep extends Animal {
// Sheep have something special to say:
void talk() {
println("baa");
}
}

ArrayList list = new ArrayList();

list.add(new Cow());
list.add(new Cow());
list.add(new Sheep());
list.add(new Sheep());

Iterator it = list.iterator();
while (it.hasNext()) {
Animal animal = (Animal)it.next();
animal.talk();
}

// prints:
// moo
// moo
// baa
// baa



The basic idea of 'extends' is that everything in your ArrayList is now of the same basic type - Animal - and that means you can treat them in the same way, and you don't care what special type they are or what special thing they do.

Once you're comfortable with this idea, you might like to read up on how to use the extra Java keywords interface and abstract which can make things clearer, especially when you're working on the same code with other people.  They can be thought of as a contract, or promise, to the compiler -

Code:


abstract class Animal {
abstract void talk();
}



Means "I promise that things of type Animal will have a talk method", for example.  Because Animal is abstract you can't create it directly, you must provide a subclass which extends Animal and implements the talk method.  Interfaces are very similar but more restrictive on what Animal can provide for itself, and also a bit fussier to use within Processing.
Re: How to use InstanceOf?
Reply #3 - May 22nd, 2006, 1:44pm
 
...and good programming practice flows in TomC's wake. Smiley

Nice explanation, Tom.
Re: How to use InstanceOf?
Reply #4 - May 22nd, 2006, 3:06pm
 
Im overwhelmed by the quick and precise answers. Thank you, this was just what I needed.

One follow up question:

What is the difference in using:

Code:
Iterator it = list.iterator(); 
while (it.hasNext()) {  
 Animal animal = (Animal)it.next();  
 animal.talk();
}


instead of:

Code:
for (int i=0;i<list.size();i++){  
 Animal animal = (Animal) list.get(i);  
 animal.talk();
}


Is it less processor intensive or just more convenient?

How do you mark text as code?

Best greetings
~Carl Email
Re: How to use InstanceOf?
Reply #5 - May 22nd, 2006, 3:34pm
 
This forum uses bbcode - it's a little bit like HTML but with square brackets.  Use code tags to mark code blocks.  I also find it useful to mark keywords with tt tags, but I'm not very consistent about it.

I've never worked out a way to escape bbcode in a forum post to help explain it though :)

On the question of using Iterators or a for loop with .size() and .get(i) - in most cases it doesn't really matter and it's just personal preference, but there are a couple of things worth knowing.

Iterators will work on all the Java collection types, even if they don't have a get method (you just don't know what order things will arrive in).

Iterators will complain (throw an Exception) if the collection is accessed simultaneously by two different threads - e.g. if you receive data over the network and add it to an ArrayList, whilst you're also looping through and drawing it.

Using a for loop with an int counter is sometimes useful because you want to know how far through the loop you are.

So... Iterators are the proper (Java and OOP) and robust (Thread safe and interchangeable) way to code things, but for loops aren't going to hurt anyone for most tasks in Processing.

Whilst I'm here, it's worth mentioning that for Processing things to be delivered as applets, your sketch will be more compatible with older virtual machines if you use Vector instead of ArrayList, and elementAt(), addElement() and removeElement() instead of get(), add() and remove().  I'm not sure if that applies to the current beta version (VM support changes depending on how many new features are added!) but it's worth bearing in mind.
Re: How to use InstanceOf?
Reply #6 - May 23rd, 2006, 9:26am
 
Perfect!

I have everything I need

Oh and Jesse, your MaxLink is wonderful!

Grey skies, but happy happy in Denmark
~Carl Email
Re: How to use InstanceOf?
Reply #7 - May 23rd, 2006, 1:53pm
 
Thanks!  Glad it's making itself useful.
Page Index Toggle Pages: 1