i am new to android. I want to know how to compare Integer with integer array. There is a set of integer array (Ex) array_int={1,2,3,4} and single integer int i=2, here i want to compare both integers,and in case if single integer appears in array integer, i want to break the process.
for(i=0;i<integerArray.length;i++){
if(singleinteger!=integerArray[i]){ // some action }
else{
// Stop the action }
In this case it compares both integer. and at the time when two integers are equal process getting break, otherwise it iteration action till the loop getting end.
For a simple solution, use:
That will break from the loop when the two values are equal. However, some purists don’t like the use of
breaksince it can introduce readability issues, especially if yoursome actionis large (in terms of lines of code) since that removes an exit condition far away from theforitself.To fix that, you may want to consider reversing the sense of the
ifstatement so that that exit condition is closer to thefor:and that will also let you remove the
else(that’s theelseitself, not its contents) since it’s not needed any more.But, if you’re going to do that, you may as well incorporate it totally into the
forand be done with it: