Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 9
Registered: ‎08-12-2009
My Device: Not Specified

Best practice for accessing objects stored in vectors?

Is it better to access an object in a vector by loading it to an object reference like:

 

CustomObject myObject = (CustomObject)objectVector.elementAt(3);
myObject.doSomething();
myObject.setSomething(2);

 

 

OR by directly accessing it within the vector like:

 

((CustomObject)objectVector.elementAt(3)).doSomething();
((CustomObject)objectVector.elementAt(3)).setSomething(2);

 

 

 

Or is the cost pretty much the same regardless, and the format is simply a matter of convenience, readability, and preference?  Thoughts anyone?

Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some

Re: Best practice for accessing objects stored in vectors?

well, in both cases no any new object will be created. So both variants seem to be equal regarding to performance.

So here I would consider rather readability and convinience of use.

Got resolved! Press kudo icon!
Developer
Posts: 141
Registered: ‎02-10-2010
My Device: 9500
My Carrier: Airtel

Re: Best practice for accessing objects stored in vectors?

In the first case, the type cast would hapen only once and then the CustomObject may be used n number of times. So method one would be more efficient(though negligibly). The efficiency difference would be more if there are a lot of function calls being made on the CistomObject using the two ways: casted once & taken in an object versus accessed from vector & casted on every call.