Currently I’m implementing a Persistant Storage Object for my Blackberry Application.
It contains a Vector of Setting Objects.
My current implemention to get specific settings value looks like this
public String getSettingByName(String key)
{
String value = "";
for ( Enumeration e = _appSettings.elements(); e.hasMoreElements();)
{
if(((AppSettingsObject)e.nextElement()).get_key() == key)
{
value = ((AppSettingsObject)e.nextElement()).get_value();
}
}
return value;
}
Is there a better way to access that value than looping the enumeration?
Thanks!
A Vector is basically just a growable array, so you have to search through it to find any specific element. You could make sure that the vector is sorted, which would allow you to perform a binary search. I doubt it would make much difference here, since it seems unlikely that you’ll have enough elements in your vector to justify the slight increase in performance over the complexity of the code.
It seems like you want to be able to map keys to values, however. In that case, the java.util.Hashtable class might be better: