02-20-2010 03:55 PM
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:
ing(); ((CustomObject)objectVector.elementAt(3)).setSomet hing(2);
Or is the cost pretty much the same regardless, and the format is simply a matter of convenience, readability, and preference? Thoughts anyone?
02-21-2010 02:15 PM
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.
02-22-2010 02:16 AM
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.