-

2011年8月4日星期四

How do I see if a certain value occurs at any point in an array in Java?

-I'm doing a little bit of Java programming :L And I want to see if a variable is in an array at any point.

So for example, I have this:



int[] anArray = new int[3];

anArray[0] = 200;

anArray[1] = 400;

anArray[2] = 600;

int variable1 = 400;



How do I make it so that I can code something to the effect of



If 'variable1' appears in any of the values at 'anArray'

{

Do something

}

else

{

do another thing

}



Thanks in advanceSet a boolean variable to false. Loop through the array comparing each value to the value you are looking for, if it matches, set the boolean variable to true. Then do an 'if' on that boolean variable.

没有评论:

发表评论