I have an array of objects. There is a getCountry() in the object. I want to set a flag when the getCountry() is equal to say ‘XXX’.
As of know, I am doing,
boolean isXXX=false;
if(custAcctArray != null)
{
for(int index=0;index<custAcctArray.length;index++)
{
if(custAcctArray[i].getCountry().equalsIgnoreCase("XXX"))
{
isXXX=true;
}
}
}
if(isXXX)
{
Do something about it....
}
I somehow don’t like this logic when I assume the array is filled with some 100 or odd objects. Can someone throw or shed light at other ways of achieving the final output in an efficient way?
What I want: set a flag when getCountry() == “XXX“
Do you see the break; ? This exits the big loop as soon as possible. If you dont, the next iteration could set it to other value.
You can also use a “separate” array outside the object to access it quicker(1 less bounds checking)
A better way: when setting the country values, check that if “xxx” then set isXXX rightaway without needing to use a “check” algortihm 🙂