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 › . operator with ArrayList.get()
Page Index Toggle Pages: 1
. operator with ArrayList.get()? (Read 512 times)
. operator with ArrayList.get()?
Dec 12th, 2008, 10:10am
 
why doesn't this work? i thought get() returns an object. so why can't i use . to access it's methods?

Code:

ArrayList foo;
Bar blah;

void setup() {
foo = new ArrayList();
blah = new Bar();
foo.add(blah);
foo.get(0).cha();
}

class Bar {
public void cha(){
println("Cha cha cha cha");
}
}
Re: . operator with ArrayList.get()?
Reply #1 - Dec 12th, 2008, 11:04am
 
Cause Java didn't know what type your object is. You have to cast it before calling the function:

Code:

((Bar)foo.get(0)).cha();
or
Bar b = (Bar)foo.get(0);
b.cha();


You could also type your array list by using <Type> so you don't need to cast.

Code:

ArrayList<Bar> foo;
Re: . operator with ArrayList.get()?
Reply #2 - Dec 12th, 2008, 11:04am
 
because it doesn't know what type of object that it has just got.

if you cast the return value from foo.get(0) then it'll work

Bar bar = foo.get(0);
bar.cha();

or, if you want to be obtuse:

((Bar)foo.get(0)).cha();  // untested 8)

java 5 lets you define the type of things in your arraylist using generics:

ArrayList<Bar> foo;
foo = new ArrayList<Bar>();

so it knows that anything retrieved from this will be a Bar (it'll also complain if you try and put non-Bar items into it)

(edit: so close... 8)
Re: . operator with ArrayList.get()?
Reply #3 - Dec 12th, 2008, 11:08am
 
In Java 1.4 (Processing still uses this syntax), when you put an object in a Collection, it becomes anonymous, it is only seen as an Object.
That's what foo.get(0) returns.
You have to cast the value returned to the original type to be able to use it:
((Bar)foo.get(0)).cha();

Since Java 1.5, we can declare ArrayList as holding only objects of a given type:
ArrayList<Bar> foo = new ArrayList<Bar>();
Then you can use directly your code, because Java knows it is actually a Bar (I think behind the scene, it just silently adds the cast...).

[EDIT] Wow, you guys are too fast! Happy
Re: . operator with ArrayList.get()?
Reply #4 - Dec 12th, 2008, 6:56pm
 
Hey thanks everyone!

I managed to get it to work with the cast and with assigning the function to a Bar object.  I couldn't get
Code:
ArrayList<Bar> foo;  

to work though. It gives an "unexpected token: void" on setup().
Re: . operator with ArrayList.get()?
Reply #5 - Dec 12th, 2008, 9:25pm
 
eskimoblood was mistaken, and I wasn't precise enough: by "Processing still uses this syntax", I meant it doesn't support, yet, the <T> syntax. See Bug 598 "Update ANTLR grammars to support Java 1.5 syntax "
Page Index Toggle Pages: 1