 |
Author |
Topic: Vector of objects (Read 757 times) |
|
gabon
|
Vector of objects
« on: Dec 20th, 2004, 10:27pm » |
|
Hi guys I'm trying to use Vectors and I found a problem. Considering a class like: Code: class RefObj { int id; color col; // RefObj(int tid, color tcol){ id=tid; col=tcol; } } |
| and a population like: Code: RefObj obj; for(int i=0; i<10; i++){ obj=new RefObj(i,pixels[i]); testVect.add(obj); } |
| Hot to have access to a property of the object? Code: for(int i=0; i<10; i++){ println(testVect.get(i).id); } |
| It says: no field named "id" was found in type "java.lang.Object". Thx for any help, chr
|
|
|
|
fjen
|
Re: Vector of objects
« Reply #1 on: Dec 20th, 2004, 10:43pm » |
|
just "cast" it back to what it's supposed to be (... or is, actually). side note: "obj" is not a good name since it seems to be used internally by processing. and: you don't need to create an object in a variable to put that into the vector. you can directly create it inside when doing vec.add(new myObj()); Code: class RefObj { int id; color col; RefObj(int tid, color tcol){ id=tid; col=tcol; } } Vector testVect; void setup() { testVect = new Vector(); RefObj obj; } void draw() { for(int i=0; i<10; i++){ testVect.add( new RefObj(i,pixels[i]) ); } for(int i=0; i<10; i++){ println(((RefObj)testVect.get(i)).id); } } |
| /F
|
|
|
|
gabon
|
Re: Vector of objects
« Reply #2 on: Dec 21st, 2004, 12:19am » |
|
yes, the cast... I'm happy to come back on these strongly typed rules Thank u florian, chr
|
|
|
|
fry
|
Re: Vector of objects
« Reply #3 on: Dec 21st, 2004, 2:34am » |
|
on Dec 20th, 2004, 10:43pm, fjen wrote:side note: "obj" is not a good name since it seems to be used internally by processing. |
| not that i know of, is it giving you an error
|
|
|
|
fjen
|
Re: Vector of objects
« Reply #4 on: Dec 21st, 2004, 3:19am » |
|
ha, right! you got me there. i got fooled by the error-message. should have read it 'til the end ... "... However there is an accessible field ..." /F
|
|
|
|
|