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 › Vector trouble...
Page Index Toggle Pages: 1
Vector trouble... (Read 939 times)
Vector trouble...
Aug 4th, 2005, 5:52am
 
Hi! I'm a little confused about this. Say i do this:
Code:
Vector v;
for(int i=0; i<200; i++){
v.add(new Item());
}

How would I gain access to a method contained in one of those Items? I've tried:
Code:
v.get(index).methodName(); 


but keep getting
Quote:
Semantic Error: No accessible method with signature "methodName()" was found in type "java.lang.Object"

I'm kinda stumped. Any help would be appreciated!
Re: Vector trouble...
Reply #1 - Aug 4th, 2005, 10:52am
 
The get() method of Vector returns an Object, so to get your Item back you must cast it first:

Code:

Vector v;
for(int i=0; i<200; i++){
v.add(new Item());
}

for(int i=0; i<v.size(); i++){
Item item = (Item)v.get(i);
item.methodName();
}


Unfortunately, to do it on one line now looks like this:

Code:

((Item)v.get(i)).methodName();


It's one of the long standing uncomfortable aspects of Java, but it's one that becomes familiar with time.  There are various syntactical ways this is solved in the latest versions of the Java language, but they aren't in Processing yet.

It's also worth noting that if you want Vector to work across all versions of Java, you should use elementAt instead of get, and addElement/removeElement instead of add/remove.  I'm not sure what version of Java the latest version of Processing targets though, so that might be irrelevant.


Re: Vector trouble...
Reply #2 - Aug 4th, 2005, 8:27pm
 
Thank you! I would have never discovered that. This will help me with a lot of things, actually.. Thanks again Smiley
Page Index Toggle Pages: 1