7 Replies - 6091 Views - Last Post: 16 July 2011 - 10:08 PM Rate Topic: -----

#1 bizhat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

I made a code

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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • 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.
Was This Post Helpful? 2
  • +
  • -

#3 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • 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.
Was This Post Helpful? 2
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2552
  • View blog
  • Posts: 10,635
  • 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

Was This Post Helpful? 3
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Check if a Number is in Array

Posted 16 July 2011 - 08:52 PM

View Postg00se, 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 :^:
Was This Post Helpful? 1
  • +
  • -

#6 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • 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.
Was This Post Helpful? -1
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Check if a Number is in Array

Posted 16 July 2011 - 09:08 PM

View PostHiram, 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

Was This Post Helpful? 1
  • +
  • -

#8 Hiram  Icon User is offline

  • D.I.C Head

Reputation: 69
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1