# Check if a Number is in Array

Page 1 of 1

## 7 Replies - 35691 Views - Last Post: 16 July 2011 - 10:08 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=239725&amp;s=a733b013a9dfd3692bec2e13314353e8&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bizhat

Reputation: 0
• Posts: 5
• Joined: 01-July 11

# Check if a Number is in Array

Posted 16 July 2011 - 04:54 AM

I wanted to check if a number is in an Array

public class Test
{

public static void main(String[] args) throws InterruptedException, Exception
{

int[] VALID_SATES = { 8132, 33, 8988, 1740 };
int currentState = 331;

boolean valid = checkInArray(currentState, VALID_SATES);

if (valid)
{
System.out.print("Value in Array");
}
else
{
System.out.print("Value Not In Array");
}
}

private static boolean checkInArray(int currentState, int[] myArray)
{

for (int i : myArray )
{
if (i == currentState)
{
return true;
}
}

return false;
}
}

I am new to java, so not sure if there an easy way to do this ?

Is This A Good Question/Topic? 0

## Replies To: Check if a Number is in Array

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Check if a Number is in Array

Posted 16 July 2011 - 05:38 AM

As a learning point, you could consider

A. sorting the array using sort( int[] a ) and then using the binarySearch( int[] a ), or
B. converting to a List and using List.contains( Object )

but I don't know that those are easier or more efficient than what you've done for a problem of the scope you've shown. If the problem was more complex, perhaps using method A or B above would make sense.

### #3 ipushmycar

• D.I.C Regular

Reputation: 86
• Posts: 390
• Joined: 29-August 10

## Re: Check if a Number is in Array

Posted 16 July 2011 - 06:52 AM

If you are new I would stick with what you have done.

### #4 g00se

• D.I.C Lover

Reputation: 3541
• Posts: 16,135
• Joined: 20-September 08

## Re: Check if a Number is in Array

Posted 16 July 2011 - 11:33 AM

As a point of good practice, a method should, wherever possible, have only one exit point. Consider coding this as follows. Yes the for loop is a little more cumbersome, but you can console yourself with the fact that it's faster and more efficient than a foreach:

private static boolean checkInArray(int currentState, int[] myArray) {
boolean found = false;

for (int i = 0; !found && (i < myArray.length); i++) {
found = (myArray[i] == currentState);
}

return found;
}

This post has been edited by g00se: 16 July 2011 - 11:35 AM
Reason for edit:: format

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Check if a Number is in Array

Posted 16 July 2011 - 08:52 PM

g00se, on 16 July 2011 - 02:33 PM, said:

As a point of good practice, a method should, wherever possible, have only one exit point. Consider coding this as follows. Yes the for loop is a little more cumbersome, but you can console yourself with the fact that it's faster and more efficient than a foreach:

private static boolean checkInArray(int currentState, int[] myArray) {
boolean found = false;

for (int i = 0; !found && (i < myArray.length); i++) {
found = (myArray[i] == currentState);
}

return found;
}

"as a point of good practice"....
we can start a whole debate about that but

I really prefer YOUR way of doing it... going out with a return as soon as the condition is met

### #6 Hiram

Reputation: 69
• Posts: 203
• Joined: 02-June 09

## Re: Check if a Number is in Array

Posted 16 July 2011 - 08:57 PM

I'm with PBL. If that array had 50,000 integers, and the integer being searched for was the first element, that's 49,999 iterations performed for no reason.

A great man once told me, never make assumptions about your data. This includes the size.

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Check if a Number is in Array

Posted 16 July 2011 - 09:08 PM

Hiram, on 16 July 2011 - 11:57 PM, said:

I'm with PBL. If that array had 50,000 integers, and the integer being searched for was the first element, that's 49,999 iterations performed for no reason.

A great man once told me, never make assumptions about your data. This includes the size.

Totally false g00se algorithn would not for 50,000 integer as you Hiram mentionned his for() loop check for condition watch
private static boolean checkInArray(int currentState, int[] myArray) {
boolean found = false;

for (int i = 0; !found <----- here && (i < myArray.length); i++) {
found = (myArray[i] == currentState);
}

return found;
}

which is equivalent to
private static boolean checkInArray(int currentState, int[] myArray) {
boolean found = false;

for (int i = 0; i < myArray.length; i++) {
if(myArray[i] == currentState) {
found = true;
break;
}
}

return found;
}

But, in the other hand, I am like you, I hate multiple tests in a single for() ... I would have written it that way

private static boolean checkInArray(int currentState, int[] myArray) {
int i = 0;

for (; i < myArray.length); i++) {
if(myArray[i] == currentState)
break;
}

return i != myArray.length;
}

This post has been edited by pbl: 16 July 2011 - 09:12 PM

### #8 Hiram

Reputation: 69
• Posts: 203
• Joined: 02-June 09

## Re: Check if a Number is in Array

Posted 16 July 2011 - 10:08 PM

Oh! I didn't see the found variable there. I don't know what I was thinking. My mistake!

That'll teach me to read code first thing in the morning, haha.